4 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
這是一個(gè)更新版本,旨在完全滿足上述要求:
inp={"list": [
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"html": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"html": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"react": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"vue": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"vue": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"html": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"html": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"react": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"vue": {"script": ""}}}
]};
const res =Object.values(inp.list.reduce((a,c)=>{
// Object.values: only return the different *values* of the collection
let nam=c.name+c.path+c.type; // collect "similar" objects together
let curr=a[nam]=a[nam]||{...c}; // create or refer to a collection element
Object.entries(c.data).forEach(([k,v])=>curr.data[k]={...curr.data[k],...v})
return a},{}));
console.log(res)
以前的版本...
這是您問題的另一種解決方案(單行)。它與您的要求不同,因?yàn)樗鼧?gòu)建在輸入對(duì)象的第一個(gè)元素上,因此不包含名稱“l(fā)ight_with_header_and_icons”而不是“l(fā)ight_with_header”,
const inp={"list": [
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"html": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"html": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"react": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"vue": {"script": ""}}},
{"name": "light_with_header","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"vue": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"html": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"angular": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"html": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"html": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"react": {"script": ""}}},
{"name": "light_with_header_and_icons","path": "webapp/master_layouts/sidebar_layouts/","type": "Directory","data": {"vue": {"script": ""}}}
]}
const res=
inp.list.reduce((a,c)=>(Object.entries(c.data).forEach(([k,v])=>a.data[k]={...a.data[k],...v}),a))
console.log(res)
// alternative version:
const res2=
inp.list.reduce((a,c)=>(Object.entries(c.data).forEach(([k,v])=>a.data[k]={...a.data[k],...v}),a),inp.list[inp.list.length-1])
console.log(res2)
我的“替代”版本返回一個(gè)基于 的最后一個(gè)元素構(gòu)建的輸出對(duì)象inp.list
。

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
首先維護(hù)一個(gè)將 id 映射到對(duì)象的對(duì)象,然后您可以稍后將其轉(zhuǎn)換回列表:
const mappings = {};
const ordering = []; // maintain ordering and path/type fields
// assuming your object is in variable obj
for (const {data, ...rest} of obj.list) {
if (rest.name in mappings) {
Object.assign(mappings[rest.name], data);
} else {
mappings[rest.name] = data;
ordering.push(rest);
}
}
const newObj = {list: ordering.map(x => ({...x, data: mappings[x.name]}))};
const obj = {
"list": [
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"html": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"html": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"react": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"vue": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"vue": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"html": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"html": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"react": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"vue": {
"script": ""
}
}
},
]
};
const mappings = {};
const ordering = []; // maintain ordering and path/type fields
// assuming your object is in variable obj
for (const {data, ...rest} of obj.list) {
if (rest.name in mappings) {
Object.assign(mappings[rest.name], data);
} else {
mappings[rest.name] = data;
ordering.push(rest);
}
}
const newObj = {list: ordering.map(x => ({...x, data: mappings[x.name]}))};
console.log(newObj);
.as-console-wrapper {min-height: 100%};

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是檢查現(xiàn)有條目的簡(jiǎn)單reduce()方法。find()
const input = { "list": [{ "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "angular": { "html": "" } } }, { "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "angular": { "script": "" } } }, { "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "html": { "html": "" } } }, { "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "html": { "script": "" } } }, { "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "react": { "script": "" } } }, { "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "vue": { "script": "" } } }, { "name": "light_with_header", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "vue": { "script": "" } } }, { "name": "light_with_header_and_icons", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "angular": { "html": "" } } }, { "name": "light_with_header_and_icons", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "angular": { "script": "" } } }, { "name": "light_with_header_and_icons", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "html": { "html": "" } } }, { "name": "light_with_header_and_icons", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "html": { "script": "" } } }, { "name": "light_with_header_and_icons", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "react": { "script": "" } } }, { "name": "light_with_header_and_icons", "path": "webapp/master_layouts/sidebar_layouts/", "type": "Directory", "data": { "vue": { "script": "" } } },] }
const output = {};
output.list = input.list.reduce((a, o) => {
const p = a.find(({ name }) => name === o.name);
if (p) {
Object.entries(o.data).forEach(([k, v]) =>
p.data[k] = { ...p.data[k] ?? {}, ...v }
);
} else {
a.push({ ...o })
}
return a;
}, []);
console.log(output)

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
我已經(jīng)找到了我需要的解決方案。真的謝謝大家的幫助。我把車改裝了10米。我創(chuàng)建一個(gè)新列表并將所有內(nèi)容附加到其中。我的未來代碼..
let myList = []
list.reduce((a,c)=>{
if(a.name !== c.name){
myList.push(c)
return c
}else{
return (Object.entries(c.data).forEach(([k,v])=>a.data[k]={...a.data[k],...v}),a)
}
},list[list.length-1])
添加回答
舉報(bào)