
- 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
Find average of each array within an array in JavaScript
We are required to write a JavaScript function that takes in an array of arrays of Numbers. The function should create a new array, compute the average for each subarray and push it at the corresponding indices in the new array.
Let’s say the following is our array −
const arr = [ [1], [2,3], [4,5,6,7] ];
Example
The code for this will be −
const arr = [ [1], [2,3], [4,5,6,7] ]; const average = (arr = []) => { const sum = arr.reduce((a, b) => a + b); return sum / arr.length; } const averageWithin = (arr = []) => { const res = arr.map(el => average(el)); return res; }; console.log(averageWithin(arr));
Output
And the output in the console will be −
[ 1, 2.5, 5.5 ]
- Related Articles
- Find average of each array within an array JavaScript
- Calculating average of an array in JavaScript
- Number of vowels within an array in JavaScript
- Chunking array within array in JavaScript
- Finding confusing number within an array in JavaScript
- Counting possible APs within an array in JavaScript
- Realtime moving average of an array of numbers in JavaScript
- JavaScript: Computing the average of an array after mapping each element to a value
- Update an array of strings nested within an array of objects in MongoDB
- Finding the third maximum number within an array in JavaScript
- Get average of every group of n elements in an array JavaScript
- Average of array excluding min max JavaScript
- How to redundantly remove duplicate elements within an array – JavaScript?
- Filtering array within a limit JavaScript
- PHP program to find standard deviation of values within an array

Advertisements