Javascript sum array
In this article, we will take a look at different ways to find sum of array elements in javascript with example code and explanation
Javascript Array object has a
reduce()
function which reduces array to a single value. reduce()
applies a function to each element of the array. This function is called reducer function.
It is a callback function and takes following arguments:
1. accumulator
It stores or accumulates the return value of function in every invocation. It is updated every time the callback function is invoked.
This argument is mandatory.
2. Current value
Current array element on which callback function is called. It is mandatory.
3. Index
Index of current array element. It is the index or position of second argument above. It is optional.
4. Array
Array on which reduce()
was called. It is optional.
reduce()
itself takes 2 arguments.
A. The callback function or the reducer function as explained above.
B. Initial Value
This is the value of accumulator(first argument) of callback function when it is called for the first time.
It is optional and if not provided, accumulator is set to the first array element.
For finding the sum of array elements, reducer function is defined as below
function(a, b) { return a+b; }
It takes two arguments and returns their sum.
Sum of array elements using reduce()
function is given in below example
let arr=[1,2,3,4,5]; let sum=arr.reduce(function(a,b) { return a+b }); console.log(sum);
Callback function can also be written using ES6 arrow function as shown below
let arr=[1,2,3,4,5]; let sum=arr.reduce((a,b) => a+b); console.log(sum);
Lodash library has a
sum()
function which takes an array argument and returns the sum of its values as shown below. import l from "lodash"; let arr = [1, 2, 3, 4, 5]; console.log(l.sum(arr));
Do not forget to import object from lodash.
Method 3: Using for-of loop
A for-of
loop is used to iterate over an array. It has below syntax
for(let item of array) { }
where item contains an array element in every iteration.
Below code can be used to find sum of all numbers in array with a for-of
loop
let arr=[1,2,3,4,5]; // vaariable to hold sum of elements let sum=0; for(let num of arr) { // add array element sum+=num; } console.log(sum);
Method 4: Using for-in loop
Javascript for-in loop can be used to iterate an array with the below syntax
for(let index of array) { }
where index contains the index of current array element.
Array element can be accessed using array[index]
syntax.
To find sum of array elements with a for-in
loop, use below syntax
let arr=[1,2,3,4,5]; // vaariable to hold sum of elements let sum=0; for(let index in arr) { // add array element sum+=arr[index]; } console.log(sum);
Sum array of objects
Suppose you have an array of javascript objects like the one below
let arr=[ {id:1, price:1000}, {id:2, price:2000}, {id:3, price:3000} ];
and you have to find sum of one of the fields or properties of all its objects, let’s say price, in this case.
This can be done using a for-in loop as shown below.
let arr=[ {id:1, price:1000}, {id:2, price:2000}, {id:3, price:3000} ]; let sum=0; for(let item of arr) { sum+=item['price']; } console.log(sum);
Each iteration of the loop will contain the current array element as in earlier example.
Only difference is that instead of directly accessing the item to be added, we need to access it by its key in the object.