Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 合并具有相同键值对的对象数组中的值_Javascript_Arrays_Object_Lodash - Fatal编程技术网

Javascript 合并具有相同键值对的对象数组中的值

Javascript 合并具有相同键值对的对象数组中的值,javascript,arrays,object,lodash,Javascript,Arrays,Object,Lodash,我有以下数组: const arr = [ { id:"aaa", name:"aaa", type:"director", nodes:[ { type:"manager", id:"111", name:"111" } ] }, { id:"aaa", n

我有以下数组:

     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、名称和类型)对的节点对象数组的值合并到所需的输出

我用arr.reduce尝试了以下方法,但没有成功:

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;
    }, []);
任何帮助都将不胜感激。提前感谢。

您可以使用a收集具有相同
id
名称的所有项目

var数组=[{id:“aaa”,名称:“aaa”,类型:“director”,节点:[{type:“manager”,id:“111”,名称:“111”},{id:“aaa”,名称:“bbb”,类型:“director”,节点:[{type:“manager”,id:“222”,名称:“222”},{id:“aaa”,名称:“aaa”,类型:“director”,节点:[{type:“manager”,“id:“333”,名称:“333”},{id:“aaa”,名称:“bbb”,类型:“director”,节点:[{type:“manager”,id:“444”,name:“444”}],
结果=[…数组.reduce((m,o)=>{
var key=['id','name'].map(k=>o[k]).join('|');
如果(m.has(钥匙)){
m、 get(key).nodes.push(…o.nodes);
}否则{
m、 set(key,Object.assign({},o,{nodes:o.nodes}));
}
返回m;
},新映射)。值();
控制台日志(结果)

作为控制台包装{max height:100%!important;top:0;}
数组的格式无效,。。(注:很抱歉编辑,我将其回滚了,这是偶然的)通过“相同的键值对”,您的具体意思是
id
name
、和
type
,还是“除了-
节点以外的所有内容”
,或者其他什么?是的。我刚刚编辑了这个问题,我想你可以使用for-in/as循环函数来管理它。。。类似这样的伪代码:for(i=0;i)数组中的obj