
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to sum elements at the same index in array of arrays into a single array? JavaScript
We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array.
If the original array is −
[ [43, 2, 21],[1, 2, 4, 54],[5, 84, 2],[11, 5, 3, 1] ]
Then the output should be −
[60, 93, 30, 55]
Let’s write a sample function addArray()
The full code for this function will be −
Example
const arr = [ [43, 2, 21],[1, 2, 4, 54],[5, 84, 2],[11, 5, 3, 1] ]; const sumArray = (array) => { const newArray = []; array.forEach(sub => { sub.forEach((num, index) => { if(newArray[index]){ newArray[index] += num; }else{ newArray[index] = num; } }); }); return newArray; } console.log(sumArray(arr));
Output
The output in the console will be −
[ 60, 93, 30, 55 ]
Above, we iterate over each element of the original array and then each number, checking if the sum of that index already existed, we just added the corresponding number to it othewise we set the corresponding num equal to it.
- Related Articles
- JavaScript: Combine highest key values of multiple arrays into a single array
- Splitting array of numbers into two arrays with same average in JavaScript
- How to duplicate elements of an array in the same array with JavaScript?
- How to get single array from multiple arrays in JavaScript
- Construct an array from XOR of all elements of array except element at same index in C++
- Turning a 2D array into a sparse array of arrays in JavaScript
- Partial sum in array of arrays JavaScript
- How to add two arrays into a new array in JavaScript?
- How to add new array elements at the beginning of an array in JavaScript?
- How to sum all elements in a nested array? JavaScript
- Reverse index value sum of array in JavaScript
- How to combine two arrays into an array of objects in JavaScript?
- How to merge objects into a single object array with JavaScript?
- Absolute sum of array elements - JavaScript
- How to find the sum of all elements of a given array in JavaScript?

Advertisements