简体   繁体   中英

merge values inside array of objects with the same key-value pair

I have the following array:

     const arr = [  
   {  
      id:"aaa",
      name:"aaa",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"111",
            name:"111"
         }
      ]
   },
   {  
      id:"aaa",
      name:"bbb",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"222",
            name:"222"
         }
      ]
   },
   {  
      id:"aaa",
      name:"aaa",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"333",
            name:"333"
         }
      ]
   },
   {  
      id:"aaa",
      name:"bbb",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"444",
            name:"444"
         }
      ]
   }
]

desired output:

        const arr = [  
   {  
      id:"aaa",
      name:"aaa",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"111",
            name:"111"
         },
         {  
            type:"manager",
            id:"333",
            name:"333"
         }
      ]
   },
   {  
      id:"aaa",
      name:"bbb",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"222",
            name:"222"
         },
         {  
            type:"manager",
            id:"444",
            name:"444"
         }
      ]
   }

I am trying to merge the value of the array of objects of nodes that have the same key-value(id, name, and type) pairs in the above array to the desired output.

I have tried the following but in vain with the arr.reduce, but in vain:

arr.reduce((acc, el) => {
      var existEl = acc.find(e => e.nodes.id === el.nodes.id);
      if (existEl) {   
        existEl = el;
      } else {
        acc.push(el);
      }

      return acc;
    }, []);

Any help would be much appreciated. Thanks in advance.

You could use a Map and collect all items with same id and name

 var array = [{ id: "aaa", name: "aaa", type: "director", nodes: [{ type: "manager", id: "111", name: "111" }] }, { id: "aaa", name: "bbb", type: "director", nodes: [{ type: "manager", id: "222", name: "222" }] }, { id: "aaa", name: "aaa", type: "director", nodes: [{ type: "manager", id: "333", name: "333" }] }, { id: "aaa", name: "bbb", type: "director", nodes: [{ type: "manager", id: "444", name: "444" }] }], result = [...array.reduce((m, o) => { var key = ['id', 'name'].map(k => o[k]).join('|'); if (m.has(key)) { m.get(key).nodes.push(...o.nodes); } else { m.set(key, Object.assign({}, o, { nodes: o.nodes })); } return m; }, new Map).values()]; console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM