Syntax, Parameter and Return Value
Syntax:
_.meanBy(array, [iteratee=_.identity])
Parameters:
-
array (Array): The array to iterate over.
- [iteratee=_.identity] (Function|Object|String): The iteratee invoked per element to generate the criterion by which the mean is calculated.
Return Value:
(Number) - Returns the mean of the array values based on the iteratee.
Examples
Calculating Average of a Specific Property:
JavaScript
var _ = require('lodash');
var products = [
{ name: 'Product 1', price: 100 },
{ name: 'Product 2', price: 200 },
{ name: 'Product 3', price: 150 }
];
var averagePrice = _.meanBy(products, (p) => p.price);
console.log(averagePrice);

You can also try this code with Online Javascript Compiler
Run Code
Output:
150
Demonstrates calculating the average price of products.
Using Property Name Shorthand:
JavaScript
var employees = [
{ name: 'John', age: 28 },
{ name: 'Jane', age: 34 },
{ name: 'Joe', age: 26 }
];
var averageAge = _.meanBy(employees, 'age');
console.log(averageAge);

You can also try this code with Online Javascript Compiler
Run Code
Output:
29.333...
Shows the use of property name shorthand for calculating the average age.
Average Calculation in Data Analysis:
JavaScript
var data = [
{ month: 'January', sales: 80 },
{ month: 'February', sales: 120 },
{ month: 'March', sales: 100 }
];
var averageSales = _.meanBy(data, 'sales');
console.log('Average Sales:', averageSales);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Average Sales: 100'
An example of using _.meanBy() in a data analysis context to find average sales.
Handling Complex Structures:
JavaScript
var measurements = [
{ item: 'Item1', dimensions: { length: 10, width: 5 }},
{ item: 'Item2', dimensions: { length: 15, width: 10 }}
];
var averageLength = _.meanBy(measurements, item => item.dimensions.length);
console.log('Average Length:', averageLength);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Average Length: 12.5'
Demonstrates calculating the average length from a complex data structure.
Frequently Asked Questions
How does _.meanBy() handle non-numeric values?
_.meanBy() ignores non-numeric values when calculating the mean. If the iteratee function returns non-numeric values, they may affect the calculation, leading to unexpected results or NaN.
What happens if the array is empty or the property is missing?
If the array is empty or the specified property does not exist on any object in the array, _.meanBy() returns NaN.
Can _.meanBy() be used with arrays of simple values?
For arrays of simple values (like numbers), _.mean() is more suitable. _.meanBy() is specifically designed for arrays of objects.
Conclusion
Lodash's _.meanBy() method is a valuable tool for calculating the average value of a specific property in an array of objects. It provides a concise and effective solution for statistical computations and data analysis involving complex data structures.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.