let arr1 = [
{ type:1, name:"", age:"", num:"a" },
{ type:2, name:"", age:"", num:"b" },
{ type:3, name:"", age:"", num:"c" },
{ type:4, name:"", age:"", num:"d" }
];
let arr2 = [
{ type:1, name:"1", age:"45" },
{ type:3, name:"2", age:"4" },
{ type:2, name:"4", age:"4" },
{ type:4, name:"5", age:"4" }
];
// 根据 type属性对应,最终合并成
let endArr = [
{ type:1, name:"1", age:"45",num:"a" },
{ type:2, name:"2", age:"4", num:"b" },
{ type:3, name:"4", age:"4", num:"c" },
{ type:4, name:"5", age:"4", num:"d" }
]
我的思路,先把两个数组合成一个,变成一个分组问题,然后对同一组进行合并,最后对每一组的属性值进行重新赋值;
指定属性是唯一的 key,可以这样做: