What is the best way to condense and transform this object array into a new array?
Here is the data I am working with:
var objArray =
[{
state: 'NY',
type: 'A',
population: 100
},
{
state: 'NY',
type: 'A',
population: 300
},
{
state: 'NY',
type: 'B',
population: 200
},
{
state: 'CA',
type: 'A',
population: 400
},
{
state: 'CA',
type: 'A',
population: 400
}];
In cases where an entry shares both the same state
AND type
, I want to merge them into one entry while adding up their populations.
Finally, I aim to map this condensed data into an array structured like this:
var outputArray = [ ['A', 'NY', 400 ], ['B', 'NY', 200], ['A', 'CA', 800] ]