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

javascript - How to sum property values of an object? - Stack Overflow

programmeradmin1浏览0评论

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

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);
Share Improve this question edited Nov 11, 2016 at 15:51 trincot 350k36 gold badges271 silver badges322 bronze badges asked Jul 23, 2016 at 9:24 Selvi PSelvi P 1511 gold badge1 silver badge7 bronze badges 2
  • initialize total_value as 0 – Ajay Narain Mathur Commented Jul 23, 2016 at 9:26
  • no need for for loop try this var PieData = [{ value: 25515512, color: "#00a65a", highlight: "#00a65a", label: "Received Fund" }, { value: 916952499, color: "#f56954", highlight: "#f56954", label: "Pending Fund" }]; var sum = 0; $.each(PieData, function(index, val) { sum += val.value; }); console.log(sum) – guradio Commented Jul 23, 2016 at 9:26
Add a comment  | 

5 Answers 5

Reset to default 19

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

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>

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)

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"];
}

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>

发布评论

评论列表(0)

  1. 暂无评论