How to sum property values of an object?

后端 未结 5 1088
时光说笑
时光说笑 2021-01-20 00:46

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



        
5条回答
  •  深忆病人
    2021-01-20 01:32

    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)

提交回复
热议问题