请问[{"id":"1"},{"id":"2"}]怎么与[{"user":"admin"},{"user":"admin2"}]合并成[{"id":"1","user":"admin"},{"id":"2","user":"admin2"}]
3 回答
不负相思意
TA贡献1777条经验 获得超10个赞
var array1 = [{"id":"1"},{"id":"2"}];
var array2 = [{"user":"admin"},{"user":"admin2"}];
array1.map(function(elem, index) {
elem = Object.assign(elem,array2[index]);
return elem;
})
console.log(array1);
ITMISS
TA贡献1871条经验 获得超8个赞
let arr1=[{"id":"1"},{"id":"2"},{"id":"3"}];
let arr2=[{"user":"admin"},{"user":"admin2"}];
const len = arr1.length > arr2.length ? arr1.length : arr2.length;
let arr3=[];
for(let i=0;i<len;i++){
arr3.push(Object.assign(arr1[i] || {},arr2[i] || {}))
}
添加回答
举报
0/150
提交
取消