I would like to merge similar objects and combine the quantity.
My array:
result = [
{itemNumber:'1288',quantity:700},
{itemNumber:'3298',quantity:1000},
{itemNumber:'1288',quantity:300}
]
What I did:
result = result.reduce((sum, val) => {
for (let i in sum) {
if (sum[i].itemNumber === val.itemNumber) {
return sum
}
}
sum.push(val);
return sum;
}, []);
My expected result:
result = [
{itemNumber:'1288',quantity:1000},
{itemNumber:'3298',quantity:1000},
]
If there is a way to do it with lodash I would like to know because I could not find one.
Thanks for your help.