How to compute the average of an array after mapping each element to a value in JavaScript ? Last Updated : 03 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Given an array, the task is to compute the average of an array after mapping each element to a value. Input : arr=[2, 3, 5, 6, 7] Output: 4.6 Explanation : (2+3+5+6+7)/5 = 4.6 Input : [5,7,2,7,8,3,9,3] Output: 5.5 Explanation : (5+7+2+7+8+3+9+3)/8 = 5.5 Here are some common approaches: Table of Content Using foreach() loopUsing for loop with ParseInt() methodUsing reduce() functionUsing for...of LoopUsing foreach() loopIterate the array elements using foreach() loop.Store the sum of each element in a variable.The average of all elements will be sum/length where length is the size of the given array.Example: JavaScript let arr = [2, 3, 5, 6, 7]; // Function to calculate the average of numbers function avg(arr) { let sum = 0; // Iterate the elements of the array arr.forEach(function (item, idx) { sum += item; }); // Returning the average of the numbers return sum / arr.length; } console.log(avg(arr)); Output4.6 Using for loop with ParseInt() methodIterate the numbers of the array using for loopUse ParseInt() function to parse the numbers in decimal format.Store the sum of numbers in a variable.The average of all elements will be sum/length where length is the size of the given array.Example: JavaScript arr = [2, 3, 5, 6, 7]; var sum = 0; // Iterating the elements of the loop for (var i = 0; i < arr.length; i++) { // Store the sum of all numbers sum += parseInt(arr[i], 10); } // Taking the average var avg = sum / arr.length; console.log(avg); Output4.6 Output: 4.6Using reduce() functionIn this approach, We reduce i.e replace two numbers of the array with their sum in the original array.reduce() function returns a single value i.e the sum of all the numbers of the array.Store the return value in a variable (sum variable used).The average of all elements will be sum/length where length is the size of the given array.Example: JavaScript var arr = [1, 2, 3, 4]; // Callback function calculating // the sum of two numbers function check(a, b) { return a + b; } // Reducing the numbers of the array var sum = arr.reduce(check); // Calculating the average of the numbers var avg = sum / arr.length; console.log(avg); Output2.5 Using for...of LoopIn this approach, we iterate over each element in the array using the for...of loop to calculate the sum of the elements, and then compute the average. We initialize a variable sum to 0 to keep track of the sum of the array elements.The for...of loop is used to iterate over each element in the array. For each iteration, we add the current element's value to the sum.After iterating through all elements, we calculate the average by dividing the sum by the length of the array.Example: JavaScript // Define the array let arr = [5, 7, 2, 7, 8, 3, 9, 3]; // Function to calculate the average using for...of loop function avg(arr) { let sum = 0; // Iterate over the elements of the array for (let value of arr) { sum += value; } // Calculate the average return sum / arr.length; } // Call the function and log the output console.log(avg(arr)); Output5.5 Comment More infoAdvertise with us Next Article How to compute the average of an array after mapping each element to a value in JavaScript ? R rohit2sahu Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads How to Compute the Sum and Average of Elements in an Array in JavaScript? Computing the sum and average of elements in an array involves iterating through the array, accumulating the sum of its elements, and then dividing the sum by the total number of elements to get the average. This can be achieved using various techniques, from basic loops to more advanced functional 3 min read Max/Min value of an attribute in an array of objects in JavaScript In this article, we are given an array of objects and the task is to get the maximum and minimum values from the array of objects. For this approach, we have a few methods that will be discussed below. Methods to get Min/Max values:using JavaScript apply() and Array map()using JavaScript Array reduc 3 min read How to create an array of elements, ungrouping the elements in an array produced by zip in JavaScript ? We can create an array of elements by ungrouping the elements in an array produced by zip by using different methods in JavaScript such as Math.max(), array specific methods namely Array.prototype.map(), Array.prototype.reduce() and Array.prototype.forEach(). This operation can also be termed as unz 2 min read How to Convert an Array of Objects to Map in JavaScript? Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon 3 min read How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi 3 min read Like