根据键合并两个对象数组
问题描述:
我有两个数组:
数组1:
[
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
]
和数组2:
[
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
]
我需要根据 id
合并这两个数组,得到这个:
I need to merge these two arrays based on id
and get this:
[
{ id: "abdc4051", date: "2017-01-24", name: "ab" },
{ id: "abdc4052", date: "2017-01-22", name: "abc" }
]
如何在不迭代的情况下执行此操作 Object.keys
?
How can I do this without iterating trough Object.keys
?
答
你可以这样做,
let arr3 = [];
arr1.forEach((itm, i) => {
arr3.push(Object.assign({}, itm, arr2[i]));
});
console.log(arr3); // you will get the merged array.