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 bine 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] ]
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 bine 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] ]
Share
Improve this question
asked Oct 11, 2017 at 22:36
jm22jm22
1313 silver badges9 bronze badges
2
- use Array#reduce and Array#map – Jaromanda X Commented Oct 11, 2017 at 22:39
- 3 You need to show what you have tried. Stackoverflow isn't a free code writing or tutorial service. The objective here is to help you fix your code – charlietfl Commented Oct 11, 2017 at 22:41
5 Answers
Reset to default 6Well, 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 ])
I would remend using the lodash
package (it is an extremely mon 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
mand to this:
_.groupBy(objArray, function(val){
return val.state + "#" + val.type
})
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];
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 posite 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]])
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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745390634a4625645.html
评论列表(0条)