Open In App

How to Create an Array of Cumulative Sum in JavaScript ?

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This article will demonstrate how to create an array containing a cumulative sum at each index from an array of integers in JavaScript.

The Cumulative Sum is defined as the partial sum total for the given sequence and all its previous values at a certain index. It is also known as the running sum as it keeps on adding the sum of all previous values. For instance, consider the below array:

Arr = [ 2, 7, 9, 4, 3 ]

The Cumulative sum at every index will be:

0 : 2
1 : 2 + 7 = 9
2 : 2 + 7 + 9 = 18
3 : 2 + 7 + 9 + 4 = 22
4 : 2 + 7 + 9 + 4 + 3 = 25

Hence, the resulting array will be:

Cumulative Sum = [ 2, 9, 18, 22, 25 ]

Methods to Create Cumulative Sum Array

It can be done with the following methods:

  • Naive Method: Using JavaScript Loops
  • Using JavaScript array.map() Method
  • Using JavaScript array.forEach() Method
  • Using JavaScript array.reduce() Method

Method 1: Using JavaScript Loops

Looping in JavaScript is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

Syntax:

while(condition) {
// JavaScript code
}

Example: In this example, we will demonstrate how to create an array having a cumulative sum using JavaScript for loop.

Javascript




// Initialize input array
const arr =  [2,7,9,4,3];
  
// Initialize required array with first value
let cumulativeSum = [arr[0]];
let i = 1;
  
// Used while loop to iterate and
// perform transformations
while (i < arr.length) {
    cumulativeSum.push(arr[i] + cumulativeSum[i - 1]);
    i++;
}
  
// Display Output
console.log(cumulativeSum);


Output

[ 2, 9, 18, 22, 25 ]

Method 2: Using JavaScript array.map() Method

The Javascript map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is used to iterate and perform iterations over an array.

Syntax:

map((element) => { /* … */ })

Example: In this example, we will see how to create an array having a cumulative sum using JavaScript array.map() map.

Javascript




// Create a new array
const arr = [1, 3, 5, 7, 9, 11];
  
// Variable to store sum
let sum = 0;
  
// Creating final array
let cumulativeSum = []
  
// Using map to perform transformations
arr.map((e)=>{
      sum = sum + e;
      cumulativeSum.push(sum);
})
  
// Display output
console.log(cumulativeSum);


Output

[ 1, 4, 9, 16, 25, 36 ]

Method 3: Using JavaScript array.forEach() Method

The array.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.

Syntax:

array.forEach( callback( element, index, arr ), thisValue )

Example: In this example, we will create Cumulative Sum Array using JavaScript array.map() method.

Javascript




// Create an array
const arr = [1, 3, 5, 7, 9, 11];
  
// Create variable to store sum
let sum = 0;
  
// Create resulting array
let cumulativeSum = []
  
// Using forEach method to iterate
// and perform required operation
arr.forEach((e)=>{
      sum = sum + e;
      cumulativeSum.push(sum);
})
  
// Display the output
console.log(cumulativeSum);


Output

[ 1, 4, 9, 16, 25, 36 ]

Method 4: Using JavaScript array.reduce() Method

The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator. 

Syntax: 

array.reduce( function(total, currentValue, currentIndex, arr), initialValue );

Example: This example will demonstrate how we can create an array having a cumulative sum using JavaScript array.reduce() method.

Javascript




// Declare new array
const arr = [1, 3, 5, 7, 9, 11];
  
// Initialize cumulative array 
let cumulativeSum = [];
  
// Callback function to execute in reduce method
function execute(sum,element){
  sum = sum + element;
  cumulativeSum.push(sum);
  return sum
}
  
// Using array.reduce with the Callback function
arr.reduce(execute,0);
  
// Display output
console.log(cumulativeSum);


Output

[ 1, 4, 9, 16, 25, 36 ]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads