当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Merge specific array of objects with new key

问题描述:

I am trying to merge two object into one (obj1 and obj2 into objResult):

var obj1 = {

0: [{obj1key1: 'obj1value1'}, {obj1key2: 'obj1value2'}],

1: [{obj1key3: 'obj1value3'}, {obj1key4: 'obj1value4'}],

2: [{obj1key5: 'obj1value5'}, {obj1key6: 'obj2value6'}]

};

var obj2 = {

0: [{obj2key1: 'obj2value1'}, {obj2key2: 'obj2value2'}],

1: [{obj2key3: 'obj2value3'}, {obj2key4: 'obj2value4'}],

2: [{obj2key5: 'obj2value5'}, {obj2key6: 'obj2value6'}]};

var objResult = {

0: {

0: [{obj1key1: 'obj1value1'}, {obj1key2: 'obj1value2'}],

1: [{obj2key1: 'obj2value1'}, {obj2key1: 'obj2value1'}]

},

1: {

0: [{obj1key3: 'obj1value3'}, {obj1key4: 'obj1value4'}],

1: [{obj2key3: 'obj2value3'}, {obj2key4: 'obj2value4'}]

},

2: {

0: [{obj1key5: 'obj1value5'}, {obj1key6: 'obj1value6'}],

1: [{obj2key5: 'obj2value5'}, {obj2key6: 'obj2value6'}]

}

}

I tried jQuery's and lodash's merge functions but without success.

Could you advise the best way to merge these two objects into one? Either with plain JS or jquery/lodash.

网友答案:

Iterate over the keys in one of the objects, and for each one, set that key on the result object to an object containing the values from both your original objects.

function merge(obj1, obj2) {
    var objResult = {};

    Object.keys(obj1).forEach(function(key) {
        objResult[key] = {
            0: obj1[key],
            1: obj2[key]
        }
    })

    return objResult;
}

This assumes that both objects have the same set of keys. If they don't, you can iterate over the keys of both objects, and this will set undefined as the value when that object does not contain that key.

Demo

网友答案:

Assuming that the two objects are of the same size (length)

var obj1 = {
 0: [{obj1key1: 'obj1value1'}, {obj1key2: 'obj1value2'}],
 1: [{obj1key3: 'obj1value3'}, {obj1key4: 'obj1value4'}],
 2: [{obj1key5: 'obj1value5'}, {obj1key6: 'obj2value6'}]
};
var obj2 = {
 0: [{obj2key1: 'obj2value1'}, {obj2key2: 'obj2value2'}],
 1: [{obj2key3: 'obj2value3'}, {obj2key4: 'obj2value4'}],
 2: [{obj2key5: 'obj2value5'}, {obj2key6: 'obj2value6'}]};

var result = {};
_.each(obj1, function(obj, key){
   result[key] = [];
   result[key].push(obj);
   result[key].push(obj2[key]);    
});

$('body').append('<pre><code>' + JSON.stringify(result) + '</code></pre>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://lodash.com/_js/lodash.js"></script>
网友答案:

Here is a plain JS example that can manage objects with different sizes.

var obj1 = {
 0: [{obj1key1: 'obj1value1'}, {obj1key2: 'obj1value2'}],
 1: [{obj1key3: 'obj1value3'}, {obj1key4: 'obj1value4'}],
 2: [{obj1key5: 'obj1value5'}, {obj1key6: 'obj2value6'}]
};
var obj2 = {
 0: [{obj2key1: 'obj2value1'}, {obj2key2: 'obj2value2'}],
 1: [{obj2key3: 'obj2value3'}, {obj2key4: 'obj2value4'}],
 2: [{obj2key5: 'obj2value5'}, {obj2key6: 'obj2value6'}]};

var obj3 = [obj1, obj2].reduce(function(result, obj, objNum){
  return Object.keys(obj).reduce(function(result, k){
    result[k] = result[k] || {};
    result[k][objNum] = obj[k];
    return result;
  }, result);
}, {});

console.log(obj3);
分享给朋友:
您可能感兴趣的文章:
随机阅读: