Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Calculating Average of a Specific Property:
4.2.
JavaScript
4.3.
Using Property Name Shorthand:
4.4.
JavaScript
4.5.
Average Calculation in Data Analysis:
4.6.
JavaScript
4.7.
Handling Complex Structures:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.meanBy() handle non-numeric values?
5.2.
What happens if the array is empty or the property is missing?
5.3.
Can _.meanBy() be used with arrays of simple values?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.meanBy() Method

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In data processing, especially when dealing with arrays of objects, calculating the average of a specific property is a common requirement. Lodash's _.meanBy() method streamlines this process. This method allows for the computation of the mean (average) value of a property in an array of objects, based on a specific iteratee function. 

Lodash _.meanBy() Method

It's particularly useful in scenarios where you need to calculate averages from complex data structures.

Why This Function is Used

The _.meanBy() function is used to find the average value of a specific property in an array of objects. Unlike the basic _.mean() function, which calculates the mean of an array of numbers, _.meanBy() is designed to work with collections of objects where the target value for the mean calculation is a property of these objects. This method simplifies extracting and averaging these values, making it highly useful in statistical analysis and data manipulation.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass