Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum property values of an object?

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);
like image 927
Selvi P Avatar asked Jul 23 '16 09:07

Selvi P


People also ask

How do you get the sum of a property in an array of objects?

To sum a property in an array of objects:Call the reduce() method to iterate over the array. On each iteration increment the sum with the specific value. The result will contain the sum of the values for the specific property.

How do you add a property to an object?

One way is to add a property using the dot notation: obj. foo = 1; We added the foo property to the obj object above with value 1.

How do you sum values in JavaScript?

parseInt() is used to convert the user input string to number. const num1 = parseInt(prompt('Enter the first number ')); const num2 = parseInt(prompt('Enter the second number ')); Then, the sum of the numbers is computed.


3 Answers

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);
like image 100
Nina Scholz Avatar answered Oct 23 '22 15:10

Nina Scholz


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>
like image 29
Ajay Narain Mathur Avatar answered Oct 23 '22 15:10

Ajay Narain Mathur


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)
like image 39
Ibrahim Khan Avatar answered Oct 23 '22 15:10

Ibrahim Khan