I have 2 arrays of objects returning from 2 different 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,
},
];
This is my current approach but it will merge the entire object from result2 to result1 if they have the same id
const t = result2.reduce((acc, curr) => {
acc[curr.id] = curr;
return acc;
}, {});
const d = result1.map((d) =>
Object.assign(d, t[d.id])
);
The current result is:
{
name: 'matteo',
age: 20,
id: 1,
warnings: "yes",
hobby: "tennis"
},
{
name: 'luca',
age: 24,
id: 2,
warnings: "many",
hobby: "ping pong"
},
I would like to move only the warnings prop from the second array of objects into the first array of objects where the id of object is equal
Desired output:
const result3 = [
{
name: 'matteo',
age: 20,
id: 1,
warnings: "yes"
},
{
name: 'luca',
age: 24,
id: 2,
warnings: "many"
},
];