数据例子:
const arr1 = [ { id: 1, name: 'John' }, { id: 2, name: 'Alice' }, { id: 3, name: 'Bob' } ]; const arr2 = [ { id: 2, age: 30 }, { id: 3, age: 25 }, { id: 4, age: 28 } ];
代码:
const mergedArray = arr1.map((item) => { const matchedObject = arr2.find((obj) => obj.id === item.id); return { ...item, ...matchedObject }; }); console.log(mergedArray);