
- 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
From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
Given an array of arrays, each of which contains a set of numbers. We have to write a function that returns an array where each item is the sum of all the items in the corresponding subarray.
For example −
If the input array is −
const numbers = [ [1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12] ];
Then output of our function should be −
const output = [10, 18, 50];
So, let’s write the code for this function −
Example
const numbers = [ [1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12] ]; const sum = arr => arr.reduce((acc, val) => acc+val); const sumSubArray = arr => { return arr.reduce((acc, val) => { const s = sum(val); acc.push(s); return acc; }, []); }; console.log(sumSubArray(numbers));
Output
The output in the console will be −
[ 10, 18, 50 ]
- Related Articles
- Get the smallest array from an array of arrays in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Sum of all the non-repeating elements of an array JavaScript
- Sum of all prime numbers in an array - JavaScript
- Finding degree of subarray in an array JavaScript
- Calculating the average for each subarray separately and then return the sum of all the averages in JavaScript
- Return the first duplicate number from an array in JavaScript
- Sum of all positives present in an array in JavaScript
- JavaScript Return an array that contains all the strings appearing in all the subarrays
- Combine unique items of an array of arrays while summing values - JavaScript
- Return an empty masked array of the given shape where all the data are masked in Numpy
- Return the sum of two consecutive elements from the original array in JavaScript
- Return a map representing the frequency of each data type in an array in JavaScript
- Find document in MongoDB where at least one item from an array is not in the other?
- Return an array populated with the place values of all the digits of a number in JavaScript

Advertisements