简体   繁体   English

如何在JavaScript中合并两个对象数组

[英]How to merge two array of objects in javascript

How to merge the two array of objects in one array,the given array of objects like below. 如何将两个对象数组合并为一个数组,给定的对象数组如下所示。

var arr1=[{key:2000,value:100},{key:2001,value:200},{key:3000,value:300}]
var arr2=[{key1:2000,value1:100},{key1:2001,value1:200},{key1:3000,value1:300}]

Expected output: 预期产量:

[
  {
    "key": 2000,
    "value": 100,
    "child": [
      {
        "key1": 2000,
        "value1": 100
      }
    ]
  },
  {
    "key": 2001,
    "value": 200,
    "child": [
      {
        "key1": 2001,
        "value1": 200
      }
    ]
  },
  {
    "key": 3000,
    "value": 300,
    "child": [
      {
        "key1": 3000,
        "value1": 300
      }
    ]
  }
]

UPDATED: As some other folks have mentioned, you can loop through the first array and assign the corresponding element of the second array to the child property of the appropriate object in the first array, or you can use Array.prototype.map 更新:正如其他一些人提到的那样,您可以遍历第一个数组并将第二个数组的对应元素分配给第一个数组中适当对象的子属性,或者可以使用Array.prototype.map

for example: 例如:

var newArray = arr1.map(function(cur, idx) {
    cur.child = [arr2[idx]];
    return cur;
});

It should be noted that this (and the other solutions mentioned) depend on both arrays containing the same keys, and that they are both sorted by object key. 应该注意的是,这(以及其他提到的解决方案)取决于包含相同键的两个数组,并且它们都按对象键排序。

If you're not sure that the arrays will be sorted by key, you will have to do some preprocessing. 如果不确定数组是否按键排序,则必须进行一些预处理。 You can either sort both arrays first (this is still fragile because there are no guarantees that the keys in both arrays are the same), or do something like: 您可以先对两个数组排序(这仍然很脆弱,因为不能保证两个数组中的键都相同),也可以执行以下操作:

var secondGroupByKey = {};
for (var i=0; i<arr2.length; i++) {
    secondGroupByKey[arr2[i].key1] = arr2[i];
}

and then you can safely say: 然后您可以放心地说:

var key;
for (var j=0; j<arr1.length; j++) {
    key = arr1[j].key;
    if (secondGroupByKey[key] !== undefined) {
        arr1[j].child = [secondGroupByKey[key]];
    }
}

This approach is safer than relying on the order of the arrays, but it comes with the cost of iterating over both arrays instead of just one. 这种方法比依赖于数组的顺序更安全,但是它具有在两个数组(而不只是一个数组)上进行迭代的代价。 You also might want to throw an exception or bail out another way if you find a key in arr1 that's not present in arr2 . 如果在arr1中找到arr2不存在的键,您可能还想抛出异常或以其他方式纾困。

Loop through the first array and and assign the corresponding value from the second array to child of each object of the first array. 循环遍历第一个数组,并将第二个数组中的相应值分配给第一个数组的每个对象的child对象。

for(var i = 0;i<arr1.length;i++){
    arr1[i].child=[arr2[i]];
}
arr2.forEach(function( obj, i ) {
  arr1[ i ].child = [ obj ];
});

If I understood correctly you need to put the n-th element of array B as a 'child' property of the object with the same index on array A; 如果我理解正确,则需要将数组B的第n个元素作为对象的“子级”属性,并在数组A上具有相同的索引; here's my attempt: 这是我的尝试:

function addChildren(master, child){
    return master.map(function(elem, i){
        elem.child = elem.child || [];  
        elem.child.push(child[i]);
        return elem;
    });
};

JSFiddle demo . JSFiddle演示

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

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