
- 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
Column sum of elements of 2-D arrays in 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] ]
Output
Then the output should be −
[60, 93, 30, 55]
Let’s write the function addArray() −
Example
The full code for this function will be −
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));
Here 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.
Output
The output in the console will be −
[ 60, 93, 30, 55 ]
- Related Articles
- Equality of two 2-D arrays - JavaScript
- Finding the sum of all common elements within arrays using JavaScript
- Compute sum of all elements in 2 D array in C
- Checking for the similarity of two 2-D arrays in JavaScript
- Reverse sum of two arrays in JavaScript
- Partial sum in array of arrays JavaScript
- Finding reversed index of elements in arrays - JavaScript
- Cumulative sum of elements in JavaScript
- How to sum elements of a column in MySQL?
- 8086 program to determine sum of corresponding elements of two arrays
- Maximum sum of increasing order elements from n arrays in C++
- Sum of special triplets having elements from 3 arrays in C++
- Thrice sum of elements of array - JavaScript
- Find sum of non-repeating (distinct) elements in an arrays in C++
- How to sum elements at the same index in array of arrays into a single array? JavaScript

Advertisements