I'm attempting to calculate the total population of males and females for each county. For example, if "Bomi" has both male and female populations, they should be combined. However, I'm struggling to get it to work properly.
let population = [{
"county": "Bomi",
"district": "Dowein",
"male": 6589,
"female": 6599
},
{
"county": "Bomi",
"district": "Klay",
"male": 11884,
"female": 11513
},
{
"county": "Lofa",
"district": "Senjeh",
"male": 15442,
"female": 14585
},
{
"county": "Lofa",
"district": "Suehn Mecca ",
"male": 9025,
"female": 8482
}]
let countyPopTotal = POPULATION.reduce(function(accumulator, currentValue, index) {
if (accumulator.indexOf(currentValue.county) === -1) {
let sum = 0;
sum += currentValue.male + currentValue.female
accumulator.push(sum)
}
return accumulator
}, [])
console.log(countyPopTotal);