vue合并数组问题
let arr1 = [ {
"attachment": "https://lumall.inspures.com/images/img/product/8e358701-177a-46e1-b25e-1e13fbcd92e0.jpg"
},
{
"attachment": "https://lumall.inspures.com/images/img/product/2adcd34a-786f-43ac-b8a8-3c45ed408019.jpg"
}
]
let arr2 = [ {
"attachment": "blob:http://localhost:8096/9b565718-7799-440b-b761-de747f2d59c5",
"number": 0,
"id": ""
},
{
"attachment": "blob:http://localhost:8096/1d826622-bc72-466f-8778-30dcaf773489",
"number": 1,
"id": ""
}
]
最终想要的数据格式如下:把arr2的attachment替换成arr1的attachment,感谢各位
[ {
"attachment": "https://lumall.inspures.com/images/img/product/8e358701-177a-46e1-b25e-1e13fbcd92e0.jpg",
"number": 0,
"id": ""
},
{
"attachment": "https://lumall.inspures.com/images/img/product/2adcd34a-786f-43ac-b8a8-3c45ed408019.jpg",
"number": 1,
"id": ""
}
]
回答:
arr2.map((item,index)=>{ item.attachment=arr1[index].attachment
return item
})
回答:
let newArr = arr2.map((v,k)=>({...v, ...arr1[k]}));
回答:
arr2.forEach((item,index)=>item.attachment = arr1[index].attachment)
回答:
function merge(a, b) { var ret = [];
for (var i = 0; i < a.length; ++i) {
var obj = {};
for (var key in b[i]) obj[key] = b[i][key];
for (var key in a[i]) obj[key] = a[i][key];
ret.push(obj);
}
return ret;
}
console.dir(merge(arr1, arr2));
以上是 vue合并数组问题 的全部内容, 来源链接: utcz.com/p/936629.html