我有 2 个从 2 个不同的 fetch 返回的对象数组
const result1 = [
{
name: 'matteo',
age: 20,
id: 1,
},
{
name: 'luca',
age: 24,
id: 2,
},
];
const result2 = [
{
warnings: 'yes',
hobby: "tennis",
id: 1,
},
{
warnings: 'many',
hobby: "ping pong",
id: 2,
},
];
这是我当前的方法,但如果它们具有相同的 id,它将合并从 result2 到 result1 的整个对象
const t = result2.reduce((acc, curr) => {
acc[curr.id] = curr;
return acc;
}, {});
const d = result1.map((d) =>
Object.assign(d, t[d.id])
);
目前的结果是:
{
name: 'matteo',
age: 20,
id: 1,
warnings: "yes",
hobby: "tennis"
},
{
name: 'luca',
age: 24,
id: 2,
warnings: "many",
hobby: "ping pong"
},
我只想将 warnings 属性从第二个对象数组移动到第一个对象数组,其中对象 id 相等
期望的输出:
const result3 = [
{
name: 'matteo',
age: 20,
id: 1,
warnings: "yes"
},
{
name: 'luca',
age: 24,
id: 2,
warnings: "many"
},
];
慕工程0101907
相关分类