亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基于相同嵌套鍵合并嵌套 JavaScript 對象

基于相同嵌套鍵合并嵌套 JavaScript 對象

HUH函數 2023-12-14 15:44:45
我一直在嘗試轉動以下輸入:{    "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": ""            }        }    },
查看完整描述

4 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

這是一個更新版本,旨在完全滿足上述要求:


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)

以前的版本...

這是您問題的另一種解決方案(單行)。它與您的要求不同,因為它構建在輸入對象的第一個元素上,因此不包含名稱“light_with_header_and_icons”而不是“light_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)

我的“替代”版本返回一個基于 的最后一個元素構建的輸出對象inp.list。



查看完整回答
反對 回復 2023-12-14
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

首先維護一個將 id 映射到對象的對象,然后您可以稍后將其轉換回列表:


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%};


查看完整回答
反對 回復 2023-12-14
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

這是檢查現有條目的簡單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)


查看完整回答
反對 回復 2023-12-14
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

我已經找到了我需要的解決方案。真的謝謝大家的幫助。我把車改裝了10米。我創建一個新列表并將所有內容附加到其中。我的未來代碼..


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])


查看完整回答
反對 回復 2023-12-14
  • 4 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號