- Related Questions & Answers
- 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 the sum of digits of factorial 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 the standard deviation of all pixels for each band in an image using the Pillow library
- Calculating excluded average - JavaScript
- Calculating the root mean square of all pixels for each band in an image using the Pillow library
- Calculating quarterly and yearly average through JavaScript
- Calculating average of an array in JavaScript
- Check for Subarray in the original array with 0 sum JavaScript
- Find the average of all elements of array except the largest and smallest - JavaScript
- JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
- Calculating average of a sliding window in JavaScript

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

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

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));

And the output in the console will be −

7

Advertisements