On a click event in my application I am returned highlights
- an array of features (each time of differing length). So console.log(highlights)
produces:
My objective is to return the sum of the values contained in properties.census2010_Pop2010
for each feature in the object. So far I have tried the code below but nothing is returned in the console. Any suggestions would be appreciated.
total = Object.create(null);
highlights.feature.properties.forEach(function (a) {
a.census2010_Pop2010.forEach(function (b) {
total = total + b.census2010_Pop2010;
});
});
console.log(total);
On a click event in my application I am returned highlights
- an array of features (each time of differing length). So console.log(highlights)
produces:
My objective is to return the sum of the values contained in properties.census2010_Pop2010
for each feature in the object. So far I have tried the code below but nothing is returned in the console. Any suggestions would be appreciated.
total = Object.create(null);
highlights.feature.properties.forEach(function (a) {
a.census2010_Pop2010.forEach(function (b) {
total = total + b.census2010_Pop2010;
});
});
console.log(total);
Share
Improve this question
edited May 10, 2017 at 13:42
iskandarblue
asked May 10, 2017 at 13:35
iskandarblueiskandarblue
7,52617 gold badges70 silver badges146 bronze badges
7
|
Show 2 more comments
3 Answers
Reset to default 11highlights
is an array and you should be looping over that.
var highlights = [
{properties : { census2010_Pop2010: 10}},
{properties : { census2010_Pop2010: 20}},
{properties : { census2010_Pop2010: 30}}
]
var total = highlights.reduce( function(tot, record) {
return tot + record.properties.census2010_Pop2010;
},0);
console.log(total);
If you want to use forEach it would be like this:
var highlights = [
{properties : { census2010_Pop2010: 10}},
{properties : { census2010_Pop2010: 20}},
{properties : { census2010_Pop2010: 30}}
]
var total = 0;
highlights.forEach( function(record) {
total += record.properties.census2010_Pop2010;
});
console.log(total);
This can be achieved by using reduce
to total the value of census2010_Pop2010
:
const total = highlights.reduce((acc, val) => acc + val.properties.census2010_Pop2010, 0);
Note: I have used the ES6 arrow function
to keep it concise, this is not necessary but, becoming more common when adopting a more functional style with methods like map
, filter
and reduce
.
total = 0;
highlights.forEach(function(a) {
total += Number(a.Feature.properties.census2010_Pop2010);
})
console.log(total);
You need to loop through each of the "Feature" objects in the "highlights" array. You start with a total of 0, then incrementally add the census2010_Pop2010 values from each Feature.
total
as an object? Don't you want it to be a number? – trincot Commented May 10, 2017 at 13:37census2010_Pop2010
"for each feature in the object", but eachFeature
in the object only has a singleproperties.census2010_Pop2010
entry, so "summing" doesn't come into it...? – T.J. Crowder Commented May 10, 2017 at 13:43