合并具有相同键值对的对象数组中的值 [英] merge values inside array of objects with the same key-value pair

查看:331
本文介绍了合并具有相同键值对的对象数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

     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"
         }
      ]
   }
]

所需的输出:

        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"
         }
      ]
   }

我正在尝试将上述数组中具有相同键值(id,名称和类型)对的节点对象数组的值合并到所需的输出中.

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.

我尝试了以下方法,但与arr.reduce却是徒劳的,但徒劳的:

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.

推荐答案

您可以使用 Map 并收集具有相同idname

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

这篇关于合并具有相同键值对的对象数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆