4

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:

enter image description here

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);
7
  • Why do you initialise total as an object? Don't you want it to be a number? Commented May 10, 2017 at 13:37
  • You are saying that properties is an array.... it is an object.... You should be doing forEach over the array.... Commented May 10, 2017 at 13:37
  • Total should be a number, yes Commented May 10, 2017 at 13:38
  • I would recommend using the array method reduce. Commented May 10, 2017 at 13:41
  • It's not clear what you're trying to do here. Your description doesn't match the picture of your source structure. You've said you're trying to sum census2010_Pop2010 "for each feature in the object", but each Feature in the object only has a single properties.census2010_Pop2010 entry, so "summing" doesn't come into it...? Commented May 10, 2017 at 13:43

3 Answers 3

11

highlights 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);

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2
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.

2 Comments

Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
Thanks milo526. I've updated my answer to include an explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.