简体   繁体   English

如何按键合并两个对象中的 arrays (Javascript)

[英]How to merge the arrays in two objects by key (Javascript)

I am trying to write a function that merges two objects by their key.我正在尝试编写一个 function ,它通过键合并两个对象。 The objects both have an array of numbers as their value pair and it is the arrays I want to merge.这两个对象都有一个数字数组作为它们的值对,它是我要合并的 arrays。

The input for this function is这个 function 的输入是

const obj1 = {
  foo: [1, 2],
  bar: [3],
  baz: [4],
};

const obj2 = {
  foo: [5],
  baz: [6, 7],
};

The output should be output 应该是

{
  foo: [1, 2, 5],
  bar: [3],
  baz: [4, 6, 7]
}

I tried using concat and spread operator however the answer I get is missing the "5" from the second object's foo.我尝试使用 concat 和 spread 运算符,但是我得到的答案是缺少第二个对象的 foo 中的“5”。 Also, I noticed with my current approach, if a key/value pair exists only in obj2, it will not be copied.另外,我注意到我目前的方法,如果键/值对只存在于 obj2 中,它不会被复制。 Is there a better approach to this problem?有没有更好的方法来解决这个问题? Here's my code, please help!这是我的代码,请帮忙!

function mergeByKey(obj1, obj2) {
  let resultObj = {};
  for (let key1 in obj1) {
    for (let key2 in obj2) {
      if (key1 === key2) {
        resultObj[key1] = [...obj1[key1], ...obj2[key1]];
      } else {
        resultObj[key1] = obj1[key1];
      }
    }
  }
  return resultObj;
}
**Output from code** 
{ foo: [ 1, 2 ], bar: [ 3 ], baz: [ 4, 6, 7 ] }

Not the most elegant solution, but it appears to work不是最优雅的解决方案,但它似乎有效

 function mergeObjects(obj1, obj2){ var response = {} for (let key in obj1) { response[key] = mergeProperty(obj1, obj2, key); } for (let key in obj2) { response[key] = mergeProperty(obj1, obj2, key); } return response; } function mergeProperty(obj1, obj2, property){ let response = []; if(obj1.hasOwnProperty(property)){ response = [...response, ...obj1[property]] } if(obj2.hasOwnProperty(property)){ response = [...response, ...obj2[property]] } return response; } const o1 = { foo: [1, 2], bar: [3], baz: [4], }; const o2 = { foo: [5], baz: [6, 7], }; console.log(mergeObjects(o1, o2))

You can take the distinct array of keys from both objects, then iterate that array and check for key in one object and not the other - therefore take that array, or if the key is in both objects, merge the arrays.您可以从两个对象中获取不同的键数组,然后迭代该数组并检查一个 object 而不是另一个中的键 - 因此获取该数组,或者如果键在两个对象中,则合并 arrays。

See below:见下文:

 const obj1 = { foo: [1, 2], bar: [3], baz: [4], }; const obj2 = { foo: [5], baz: [6, 7], }; //The output should be //{ // foo: [1, 2, 5], // bar: [3], // baz: [4, 6, 7] //} // keys from both objects, unique const allKeys = [... new Set(Object.keys(obj1).concat(Object.keys(obj2)))]; // iterate all keys const result = allKeys.reduce((a, c) => { if (obj1[c] &&;obj2[c]) a[c] = obj1[c]; if (.obj1[c] && obj2[c]) a[c] = obj2[c]; if (obj1[c] && obj2[c]) a[c] = obj1[c];concat(obj2[c]), return a; }. {}); console.log(result);

You could merge your two objects into one using the spread syntax ... , this will give you the keys from both objects when you use Object.keys() on the merged object.您可以使用扩展语法将两个对象合并为一个... ,当您在合并的 object 上使用Object.keys()时,这将为您提供两个对象的键。 You can then use .reduce() on this array of keys to create a new object.然后,您可以在此键数组上使用.reduce()来创建新的 object。 For each key, you can grab the array from obj1 (if it doesn't exist, default it to an empty array using || [] ), concatenated with the array from obj2 (you can also default this if it doesn't exists to an empty array):对于每个键,您可以从 obj1 获取数组(如果不存在,则使用|| []将其默认为空数组),与来自 obj2 的数组连接(如果它不存在,您也可以将其默认为到一个空数组):

 const obj1 = { foo: [1, 2], bar: [3], baz: [4], }; const obj2 = { foo: [5], baz: [6, 7], }; const merged = {...obj1, ...obj2}; const res = Object.keys(merged).reduce((acc, key) => ({...acc, [key]: (obj1[key] || []).concat(obj2[key] || []) }), {}); console.log(res);

Try something like this (assuming you don't know the object keys and that the values are always arrays).尝试这样的事情(假设您不知道 object 键并且值始终是数组)。

function merge(x,y) {
  const keys = new Set([...Object.keys(x), ...Object.keys(y)]);
  const merged = {};
  for ( const k of keys ) {
    merged[k] = [ ...(x[k] || []), ...(y[k] || []) ];
  }
  return merged;
}

you can do that...你可以这样做...

 const obj1 = { foo: [ 1, 2 ], bar: [ 3 ], baz: [ 4 ] }, obj2 = { foo: [ 5 ], baz: [ 6, 7] }, obj3 = { foo: [ 6 ], bar: [11], baz: [ 33, 14] }, obj4 = { foo: [ 88 ], truc:[ 123 ]}, obj5 = { baz: [ 116, 17] }; function mergeByKey(...objN ) { let o_names = objN.reduce((list,objArg,indx) => // objArg is obj1, obj2, objN { Object.keys(objArg).forEach( k => // make a list of object name [ objIndexs...] { // ob [ objIndexs...] on function argument if (.list[k]) list[k] = [] list[k],push(indx) }) return list }.{}) let res = {} for (let o_name in o_names ) res[o_name] = o_names[o_name],reduce((ac)=>[..,a...,objN[c][o_name]].[] ) return res } console,log( mergeByKey( obj1, obj2, obj3, obj4. obj5 )) // or just - console,log( mergeByKey( obj1, obj2 ))
 .as-console-wrapper {max-height: 100%;important:top:0}

 var obj1 = { foo: [1, 2], bar: [3], baz: [4] }, obj2 = { foo: [5], baz: [6, 7] }, merged = Object.create( obj1 ); for( var x in obj2 ) merged[ x ] = obj1[ x ].concat( obj2[ x ] ); console.log( merged );

I have solved your challenge outside a function, but it works and it is easy to implement it in a function if you would like that, and the result is as you wanted:我已经解决了您在 function 之外的挑战,但它可以工作,并且如果您愿意,很容易在 function 中实现它,结果如您所愿:

// first create an array with all the keys from both objects
let keys = Object.keys(obj1).concat(Object.keys(obj2));

// Then remove/filter the duplicates keys
let uniqueKeys = keys.filter((c, index) => {
    return keys.indexOf(c) === index;
});

//at last do the merge. us if statment to avoid concatenating undefiend elements

const obj3={};
for(let key of uniqueKeys){
  if ( Object.keys(obj1).includes(key) && Object.keys(obj2).includes(key)){
      obj3[key]= obj1[key].concat( obj2[key]);
      } else if (Object.keys(obj1).includes(key))
        {
           obj3[key]= obj1[key];
        }
          else{
            obj3[key]= obj2[key];
          }

 
   
}
 
console.log(obj3);

     ```


  [1]: https://i.stack.imgur.com/w0RbI.png

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

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