Combine same-index objects of two arrays

前端 未结 4 1164
深忆病人
深忆病人 2021-01-25 05:19

Say I have two arrays of objects, like so:

var arr1 = [{name: \'Jay\'}, {name: \'Bob\'}];
var arr2 = [{age: 22}, {age: 30}];

I want a combined

4条回答
  •  有刺的猬
    2021-01-25 06:08

    A more loosely coupled solution would be by using merge functionality of lodash library.

    This will be independent of the number of keys and key names you have in object. So in future if you decide to add keys to your object, you won't need to update the code to merge your objects.

    var _ = require('lodash');
    
    var arr1 = [{name: 'Jay'}, {name: 'Bob'}];
    var arr2 = [{age: 22}, {age: 30}];
    var arr3 = [];
    
    for (var i=0; i

提交回复
热议问题