Reputation: 131
How do I reduce this object array and and map it to a new array?
My data:
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
}];
If an entry has the same state
AND type
I need to combine it into a single entry and sum their populations.
Finally I need to map it to an array in this format.
var outputArray = [ ['A', 'NY', 400 ], ['B', 'NY', 200], ['A', 'CA', 800] ]
Upvotes: 2
Views: 916
Reputation: 357
const _ = require('lodash')
const input = [
{ 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 }
]
const expected = [
['A', 'NY', 400],
['B', 'NY', 200],
['A', 'CA', 800]
]
const output = _(input)
.groupBy(({ state, type }) => [state, type].join(':'))
.mapValues((states) => states.reduce((total, { population }) => total + population, 0))
.map((population, stateType) => {
const [state, type] = stateType.split(':')
return [type, state, population]
})
.value()
console.log(expected)
console.log(output)
Upvotes: 1
Reputation: 92461
If you know state and type will never have a certain character such as '_' you can make a key out of state
and type
such as 'NY_A' - a little like a composite key in a DB. Then you just create an object with these keys, add the populations and then pull them apart into an array:
Object.entries(
objArray.reduce((acc,curr) => (
acc[curr.type + '_' + curr.state] = curr.population + (acc[curr.type + '_' + curr.state] || 0)
, acc), {}))
.map(item => [...item[0].split('_'), item[1]])
Upvotes: 1
Reputation: 99
You could try something like this:
var arr = Object.keys(objArray).map(function (key) { return Object.keys(objArray[key]).map(function (key) { return [[objArray[0].state, objArray[0].type, objArray[0].population],[objArray[1].state, objArray[1].type, objArray[1].population]]}); })[0][0];
Upvotes: 1
Reputation: 1345
Well, first you'd want to reduce it. This can be done like so...
objArray.reduce((prev, obj) => {
if(1 + (indx = prev.findIndex(oldObj => oldObj.type === obj.type && oldObj.state === obj.state))) {
prev[indx].population += obj.population;
} else {
prev.push({...obj})
}
return prev;
}, [])
This takes the array being gathered, and modifies it in some way and returns it in the reduce callback. It will either modify an existing value's population, if it can find one with the correct state and type; or it will append a new object to the end of the array.
Now you need to map it.
.map(obj => [ obj.type, obj.state, obj.population ])
Upvotes: 6
Reputation: 4419
I would recommend using the lodash
package (it is an extremely common package in Javascript applications). It adds a lot of great functions for manipulating arrays. This post explains how to sum values on groups. You will need to modify this answer slightly to account for your two parameters, which you can do by changing the groupBy
command to this:
_.groupBy(objArray, function(val){
return val.state + "#" + val.type
})
Upvotes: 1