- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Calculating the average for each subarray separately and then return the sum of all the averages in JavaScript

Suppose, we have an array of arrays of Integers like this −

const arr = [ [1, 2, 3], [4, 5, 6] ];

We are required to write a JavaScript function that takes in one such array of arrays. The function should calculate the average for each subarray separately and then return the sum of all the averages.

Therefore, for the above array the output should be −

2 + 5 = 7

## Example

The code for this will be −

const arr = [ [1, 2, 3], [4, 5, 6] ]; const sumAverage = (arr = []) => { const average = arr.reduce((acc, val) => { const total = val.reduce((total, num) => total += num, 0); return acc += total / val.length; }, 0); return Math.round(average); }; console.log(sumAverage(arr));

## Output

And the output in the console will be −

7

- Related Articles
- Calculate the difference between the first and second element of each subarray separately and return the sum of their differences in JavaScript
- From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
- Calculating excluded average - JavaScript
- Calculating the sum of digits of factorial JavaScript
- Calculating average of an array in JavaScript
- Calculating quarterly and yearly average through JavaScript
- Calculating the median of all pixels for each band in an image using the Pillow library
- Calculating the mean of all pixels for each band in an image using the Pillow library
- Calculating the variance of all pixels for each band in an image using the Pillow library
- Calculating average of a sliding window in JavaScript
- Check for Subarray in the original array with 0 sum JavaScript
- Calculating the standard deviation of all pixels for each band in an image using the Pillow library
- Largest Sum of Averages in C++
- JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
- Calculating the root mean square of all pixels for each band in an image using the Pillow library

Advertisements