JavaScript

How to calculate the Sum of Array of Objects in JavaScript

While programming in JavaScript, you may encounter a situation where it is required to perform various operations among the array objects. For instance, in the case of keeping the data related to the same object with some modification in values, such as computing the sum of properties in an array object.

This post will discuss the process of finding and using the sum of an array’s objects using JavaScript.

How to Add/Sum Array Objects?

In JavaScript, you can utilize the following methods to calculate the sum of an array of objects:

We will now go through each of the above-mentioned methods one by one.

Method 1: Using for Loop to Find the Sum of Array of Objects

This method involves the use of the “for” loop where the array of objects is added by specifying the property on which the addition needs to be done.

Let’s check out the below-given example for a better understanding.

Example

Firstly, we will define an array of objects as follows:

var array = [
    {name: 'x', id: 1},
    {name: 'y', id: 2},
    {name: 'z', id: 3},
];

Next, we will create a variable named “add” initialized with the “0” value to avoid null value. This variable will be utilized in the for loop to store the required sum:

var add = 0;

Then, add a for loop and control its execution using the “i” iterator and the “array.length” property. Within the for loop, the sum of the values of the “id” property will be stored in the “add” variable, and after each iteration, its value gets updated:

for (var i = 0; i< array.length; i++)
{
  add += array[i].id
}

Lastly, we will print the updated added values of the array of objects on the console.

console.log( "Sum of the array values is: ", add);

The output of the above program will display “6” as the sum of the “id” property values:

Move ahead to learn the second method for finding the sum of an array of objects.

Method 2: Using reduce() Method to Find the Sum of Array of Objects

For the purpose of finding the sum of the array of objects, the “reduce()” method is also utilized. This method merges the value of the previous and current objects, which will be reduced to a single value.

Example

Firstly, we will declare an array and assign different values to the same properties of both objects:

var array = [{
"id": 1,
"roll" : 10
  }, {
"id": 2,
"roll" : 15
  }];

Next, we will compute the sum of objects in an array for the “id”, “roll” properties and reduce them to a single resultant value:

var add = array.reduce(function(previousValue, currentValue) {
return {
id: previousValue.id + currentValue.id,
roll: previousValue.roll + currentValue.roll,

    }
  });

Finally, display the resultant value of both the objects on the console screen:

console.log("The value of id becomes", add);

The resultant sum of the “id” is “3” and for “roll”, it is “25”:

We have provided the methods related to the sum of an array of objects in JavaScript.

Conclusion

To calculate the sum of an array of objects, the “for” loop and “reduce()” methods of the array class can be utilized in JavaScript. These mentioned methods assist in calculating the sum of the values of the specified object properties. Moreover, you can also perform various other operations for handling the object. This article guided about the procedure of using the sum of an array of objects in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.