I have an array of objects, where I want to find the objects having same value for name property and then merge them into single object by adding their values & subvalues.
const arr = [
{ id: 1, name: 'a', value: 2, subvalue: 3 },
{ id: 2, name: 'b', value: 4, subvalue: 3 },
{ id: 3, name: 'c', value: 4, subvalue: 3 },
{ id: 4, name: 'b', value: 3, subvalue: 4 }
]
Expected output
[
{ id: 1, name: 'a', value: 2, subvalue: 3 },
{ id: 2, name: 'b', value: 7, subvalue: 7 },
{ id: 3, name: 'c', value: 4, subvalue: 3 }
]
The id of the new object can either be 2 or it can be 4. So it doesn't matter much. I have tried using filter, but not able to find a solution which suits my requirement