I have a javascript array that looks like this
var array = [{
id: 1,
quantity: 2
},
{
id: 2,
quantity: 1
},
{
id: 1,
quantity: 5
}
]
In the above code snippet two objects has same id id:1
.
If id of two or more objects are same in the array. I want them to be converted into single object and the quantity
property of all the object with that particular id id:1
gets accumulated.
So the response array should look like this.
var resArray = [{
id: 1,
quantity: 7
},
{
id: 2,
quantity: 1
}
]