通过键,javascript组合json数组

use*_*142 13 javascript arrays algorithm json node.js

我需要结合两个json数组,由两个休息服务提供.具有相同"id"的条目属于一起.

json1 = [{id:1,name:'aaa'},
     {id:5,name:'ccc'},
     {id:3,name:'bbb'}
   ];

 json2 = [{id:3,parameter1:'x', parameter2:'y', parameter3:'z'},
     {id:1,parameter1:'u', parameter2:'v', parameter3:'w'},
     {id:5,parameter1:'q', parameter2:'w', parameter3:'e'}
    ];
Run Code Online (Sandbox Code Playgroud)

我需要在javascript中以下列方式组合/复制/克隆json数组(我的模型在angular2中):

json3 = [{id:3,name:'bbb',parameter1:'x', parameter2:'y',   parameter3:'z'},
     {id:1,name:'aaa', parameter1:'u', parameter2:'v', parameter3:'w'},
     {id:5,name:'ccc', parameter1:'q', parameter2:'w', parameter3:'e'}
    ];
Run Code Online (Sandbox Code Playgroud)

有没有办法将它们结合起来?参数名称未精确定义,需要使用可变参数向量.

我为每个循环尝试了混合.对我来说非常难看.

geo*_*org 38

两个一线:

与lodash:

res = _(json1).concat(json2).groupBy('id').map(_.spread(_.assign)).value();
Run Code Online (Sandbox Code Playgroud)

在ES2015中:

res = json2.map(x => Object.assign(x, json1.find(y => y.id == x.id)));
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法也可以合并json2中的新项目呢? (4认同)

小智 8

ES2015 georg的答案非常有用;

    json1 = [
    {id:1, test: 0},
    {id:2, test: 0},
    {id:3, test: 0},
    {id:4, test: 0},
    {id:5, test: 0}
];

json2 = [
    {id:1, test: 1},
    {id:3, test: 1},
    {id:5, test: 1}
];

json1.map(x => Object.assign(x, json2.find(y => y.id == x.id)));
Run Code Online (Sandbox Code Playgroud)

结果:

{id:1, test: 1},
{id:2, test: 0},
{id:3, test: 1},
{id:4, test: 0},
{id:5, test: 1}
Run Code Online (Sandbox Code Playgroud)


Jac*_*sen 6

如果您想编写它以便您可以接收任意数量的数组,而不仅仅是 2 个,您可以使用arguments,并执行以下操作:

var json1 = [{id:1,name:'aaa'},{id:5,name:'ccc'},{id:3,name:'bbb'}];

var json2 = [{id:3,parameter1:'x', parameter2:'y', parameter3:'z'},
             {id:1,parameter1:'u', parameter2:'v', parameter3:'w'},
             {id:5,parameter1:'q', parameter2:'w', parameter3:'e'}];

function joinObjects() {
  var idMap = {};
  // Iterate over arguments
  for(var i = 0; i < arguments.length; i++) {
    // Iterate over individual argument arrays (aka json1, json2)
    for(var j = 0; j < arguments[i].length; j++) {
      var currentID = arguments[i][j]['id'];
      if(!idMap[currentID]) {
        idMap[currentID] = {};
      }
      // Iterate over properties of objects in arrays (aka id, name, etc.)
      for(key in arguments[i][j]) {
        idMap[currentID][key] = arguments[i][j][key];
      }
    }
  }
  
  // push properties of idMap into an array
  var newArray = [];
  for(property in idMap) {
    newArray.push(idMap[property]);
  }
  return newArray;
}

var json3 = joinObjects(json1, json2);

console.log(JSON.stringify(json3));
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

这是一个有效的代码笔。


小智 5

let json1 = [
  { id: 1, name: 'aaa' },
  { id: 5, name: 'ccc' },
  { id: 3, name: 'bbb' }
];

let json2 = [
  { id: 3, parameter1: 'x', parameter2: 'y', parameter3: 'z' },
  { id: 1, parameter1: 'u', parameter2: 'v', parameter3: 'w' },
  { id: 5, parameter1: 'q', parameter2: 'w', parameter3: 'e' }
];

let result = json1.map(obj => {
  let data = json2.find(item => item.id === obj.id);
  return {...obj, ...data}
});

console.log(result);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { top: 0; max-height: 100% !important; }
Run Code Online (Sandbox Code Playgroud)