I have two arrays' of object, original and selected, sth like this:
original =[{ id: 4 , quantity: 4 },{ id: 2 , quantity: 2 },{ id: 76 , quantity: 2 }]
selected = [{ id: 2 , quantity: 1 }, { id: 100 , quantity: 7 }]
I want to be able to merge those arrays on id and if they got a similar id I should sum up the quantity,
The resulting array, in this case, should look something like this:
result=[{ id: 4 , quantity: 4 },{ id: 2 , quantity: 3 },{ id: 76 , quantity: 2 } , { id: 100 , quantity: 7 }]
I thought of doing something like this:
const result =original.map(o => ({
...selectedArray.findIndex((s) => {(s.id === o.id) && selected)? return }
...original
}));
But I am not sure how should I add to the quantity, any help or resources to look into would be appreciated.