How to sum property values of an object?

微笑、不失礼 提交于 2019-12-31 03:27:10

问题


I want to sum the property values of PieData. My expected output is 25515512+916952499 = 942468011

var PieData = [
    {
        value: 25515512,
        color: "#00a65a",
        highlight: "#00a65a",
        label: "Received Fund"     
    },
    {
        value: 916952499,
        color: "#f56954",
        highlight: "#f56954",
        label: "Pending Fund"
    }
];

Here is the script i have tried: It prints undefined value.

var total_value='';
for(var i=0;i<PieData.length;i++){
   $.each(PieData[i], function (index, val) {
       total_value += val.value;
   });
}
alert(total_value);

回答1:


You could use the native method Array#reduce for it.

var PieData = [{ value: 25515512, color: "#00a65a", highlight: "#00a65a", label: "Received Fund" }, { value: 916952499, color: "#f56954", highlight: "#f56954", label: "Pending Fund" }],
    sum = PieData.reduce(function (s, a) {
        return s + a.value;
    }, 0);

console.log(sum);

ES6

var PieData = [{ value: 25515512, color: "#00a65a", highlight: "#00a65a", label: "Received Fund" }, { value: 916952499, color: "#f56954", highlight: "#f56954", label: "Pending Fund" }],
    sum = PieData.reduce((s, a) => s + a.value, 0);

console.log(sum);



回答2:


You can use javascript forEach() method like following.

 var PieData = [
           {
               value: 25515512,
               color: "#00a65a",
               highlight: "#00a65a",
               label: "Received Fund"

           },
           {
               value: 916952499,
               color: "#f56954",
               highlight: "#f56954",
               label: "Pending Fund"
           }
    ];

var sum = 0;
PieData.forEach(function(item){
    sum += item.value;
})

console.log(sum)



回答3:


Things to change:

  • initialise total as 0 because + operator on string concatenates the values.
  • $.each loops over the object passed, So you can directly access that in callback to calculate sum.

Sample Snippet:

 var PieData = [{
   value: 25515512,
   color: "#00a65a",
   highlight: "#00a65a",
   label: "Received Fund"

 }, {
   value: 916952499,
   color: "#f56954",
   highlight: "#f56954",
   label: "Pending Fund"
 }];

 //calculating total
 var total = 0;
 $.each(PieData, function(index, value) {
   total += value.value;
 })
 alert(total)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



回答4:


PieData is an array with 2 elements, each element being a HashTable. you can sum them up by saying:

var sum = PieData[0]["value] + PieData[1]["value"]

If you have more elements or simply want to use a loop:

var sum=0;
for(var i=0;i<PieData.length;i++){
   sum+=PieData[i]["value"];
}



回答5:


var PieData = [{
        value: 25515512,
        color: "#00a65a",
        highlight: "#00a65a",
        label: "Received Fund"
    }, {
        value: 916952499,
        color: "#f56954",
        highlight: "#f56954",
        label: "Pending Fund"
    }],
    userSelectedColors = ['value'];

var totalCount = _.sumBy(userSelectedColors, _.partial(_.sumBy, PieData));

console.log(totalCount);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>


来源:https://stackoverflow.com/questions/38540176/how-to-sum-property-values-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!