Combine same-index objects of two arrays

前端 未结 4 1198
深忆病人
深忆病人 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:03

    the most trivial solution is (in compare to any util lib like underscore.js or similar). pros of it is it works without any dependency.

    var result = [];
    for(var i = 0; i < arr1.length; i+= 1) {
      item = {};
      item.name = arr1[i].name;
      item.age = arr2[i].age;
      result.push(item);
    }
    

提交回复
热议问题