最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

foreach - Sum values of a specific property in javascript object - Stack Overflow

programmeradmin2浏览0评论

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
  • Why do you initialise total as an object? Don't you want it to be a number? – trincot 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.... – epascarello Commented May 10, 2017 at 13:37
  • Total should be a number, yes – iskandarblue Commented May 10, 2017 at 13:38
  • I would recommend using the array method reduce. – evolutionxbox 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...? – T.J. Crowder Commented May 10, 2017 at 13:43
 |  Show 2 more comments

3 Answers 3

Reset to default 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);

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.

发布评论

评论列表(0)

  1. 暂无评论