简体   繁体   English

合并具有不同ID的多个对象数组

[英]Merge multiple arrays of objects with different IDs

I need to merge multiple json objects by common IDs. 我需要通过公共ID合并多个json对象。 My issue is that my objects have different keys for the ID. 我的问题是我的对象有不同的ID键。

var object1 = [
  { "name":"apples" ,"w": 1, "x": 2 },
  { "name":"banana" ,"w": 1, "x": 2 },
  { "name":"cherry" ,"w": 1, "x": 2 },
];
var object2 = [
  { "type":"banana" ,"y": 3, "x": 4 },
  { "type":"cherry" ,"y": 3, "x": 4 },
];

I would like to obtain : 我想获得:

var object1 = [
  { "name":"apples" ,"w": 1, "x": 2 },
  { "name":"banana" ,"w": 1, "x": 4, "y": 3 },
  { "name":"cherry" ,"w": 1, "x": 4, "y": 3 },
];

I want to use the same Array [object1] instead of creating a new one. 我想使用相同的Array [object1]而不是创建一个新的。 I created a codepen here 我在这里创建了一个codepen

Loop through object2 and update the fruits if found using Array.prototype.find - see demo below: 循环遍历object2并使用Array.prototype.find更新结果 - 请参阅下面的演示:

 var object1 = [{ "name":"apples" ,"w": 1, "x": 2 },{ "name":"banana" ,"w": 1, "x": 2 },{ "name":"cherry" ,"w": 1, "x": 2 }]; var object2 = [{ "type":"banana" ,"y": 3, "x": 4 },{"type":"cherry" ,"y": 3, "x": 4 }]; object2.forEach(function(e){ var found = object1.find(function(k){ return k.name === e.type; }); if(found) { found.x = ex; found.y = ey; } }); console.log(object1); 
 .as-console-wrapper{top:0;max-height:100%!important;} 

You can use reduce() to create new array and find to check if object with same name exists in object2 object with same type. 您可以使用reduce()创建新数组,并查找相同类型的object2对象中是否存在具有相同名称的对象。

 var object1 = [ { "name":"apples" ,"w": 1, "x": 2 }, { "name":"banana" ,"w": 1, "x": 2 }, { "name":"cherry" ,"w": 1, "x": 2 }, ]; var object2 = [ { "type":"banana" ,"y": 3, "x": 4 }, { "type":"cherry" ,"y": 3, "x": 4 }, ]; var result = object1.reduce(function(r, e) { var o = object2.find(a => e.name == a.type); r.push(o ? Object.assign({}, e, {x: ox, y: oy}) : e); return r; }, []) console.log(result) 

I made a solution for your problem, can you try it ? 我为你的问题找到了解决方案,你能试试吗?

 var object1 = [ { "name":"apples" ,"w": 1, "x": 2 }, { "name":"banana" ,"w": 1, "x": 2 }, { "name":"cherry" ,"w": 1, "x": 2 }, ]; var object2 = [ { "type":"banana" ,"y": 3, "x": 4 }, { "type":"cherry" ,"y": 3, "x": 4 }, ]; function mergeObject(obj1,obj2){ var obj3 = {}; for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; } return obj3; } function mergeObjectInArray(firstArray, firstArrayKey, secondaryArray, secondaryArrayKey) { var resultArray = new Array(); for(var firstArrayIndex in firstArray) { var firstArrayObject = firstArray[firstArrayIndex]; var resultArrayObject = firstArrayObject; for(var secondaryArrayIndex in secondaryArray) { var secondaryArrayObject = secondaryArray[secondaryArrayIndex]; if(firstArrayObject[firstArrayKey] === secondaryArrayObject[secondaryArrayKey]) { resultArrayObject = mergeObject(firstArrayObject,secondaryArrayObject); delete resultArrayObject[secondaryArrayKey]; } } resultArray.push(resultArrayObject); } return resultArray; } var resultArray = mergeObjectInArray(object1, "name", object2, "type"); // Assuming JSON.stringify - not available in IE<8 console.log(JSON.stringify( resultArray ) ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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