I am looking for a way to consolidate a collection of objects in javascript. For example I have a collection:
inventory = [
{count: 1, type: "Apple"},
{count: 2, type: "Orange"},
{count: 1, type: "Berry"},
{count: 2, type: "Orange"},
{count: 3, type: "Berry"}
]
What I want to end up with is:
inventory = [
{count: 1, type: "Apple"},
{count: 4, type: "Orange"},
{count: 4, type: "Berry"}
]
Is there an elegant way to do this that doesn't involve getting a list of types, searching through my collection for those types, summing the values, and making a new array with the sums?