Thursday, September 4, 2025
HomeLanguagesJavascriptLodash _.meanBy() Method

Lodash _.meanBy() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.meanBy() method is used to compute the mean value from the original array by iterating over each element in the array using the Iteratee function. It is almost the same as _.mean() function.

Syntax:

_.meanBy( array, [iteratee = _.identity] )

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: It is the array that the method iterates over to get the mean of all the elements.
  • iteratee: It is the function that is invoked for every element in the array.

Return Value: This method returns the mean value of the array.

Example 1:

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Original array 
var arr = [{ 'n': 4 }, { 'n': 2 }, { 'n': 6 }];
    
// Use of _.meanBy() method 
let mean_val =
  _.meanBy(arr, function(o) { return o.n; }); 
        
// Printing the output  
console.log(mean_val);


Output:

4

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Original array 
var arr = [{ 'n': 10 }, { 'n': 5 }, 
           { 'n': 3 }, { 'n': 12 }];
    
// Use of _.meanBy()  
// method 
let mean_val = _.meanBy(arr, 'n'); 
        
// Printing the output  
console.log(mean_val);


Output:

7.5
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6628 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS