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.
Basic Mean Calculation:
4.2.
JavaScript
4.3.
Calculating Average Score:
4.4.
JavaScript
4.5.
Using with Complex Data Structures:
4.6.
JavaScript
4.7.
Handling Empty Arrays:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.mean() handle non-numeric values in the array?
5.2.
Can _.mean() be used with large data sets?
5.3.
Is _.mean() the best choice for all average calculations?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.mean() Method

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

Introduction

Calculating the average value of an array of numbers is a common operation in data processing and statistical analysis. Lodash, a widely-used JavaScript utility library, provides the _.mean() method to simplify this task. 

Lodash _.mean() Method

This method computes the mean or average of the numbers in an array, which is especially useful in scenarios involving numerical data analysis.

Why This Function is Used

The _.mean() function is used to find the average value of an array of numbers. It sums up all the numbers in the array and divides the sum by the length of the array. This method streamlines the process of calculating the mean, providing a concise and readable solution compared to manually iterating over the array and performing the calculation.

Syntax, Parameter and Return Value

Syntax:

 _.mean(array)

Parameters:

array (Array): The array of numbers to calculate the mean.

Return Value: 

(Number) - Returns the mean of the array.

Examples 

Basic Mean Calculation:

  • JavaScript

JavaScript

var _ = require('lodash');

console.log(_.mean([4, 2, 8, 6]));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

5


Demonstrates the basic usage of _.mean() to calculate the average of an array of numbers.

Calculating Average Score:

  • JavaScript

JavaScript

var scores = [70, 85, 90, 100, 80];

var averageScore = _.mean(scores);

console.log('Average Score:', averageScore);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'Average Score: 85'


Shows calculating the average score from an array of test scores.

Using with Complex Data Structures:

  • JavaScript

JavaScript

var employees = [

 { name: 'John', salary: 50000 },

 { name: 'Jane', salary: 60000 },

 { name: 'Joe', salary: 55000 }

];

var averageSalary = _.mean(_.map(employees, 'salary'));

console.log('Average Salary:', averageSalary);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'Average Salary: 55000'


An example of calculating the mean salary from an array of objects using Lodash’s _.map().

Handling Empty Arrays:

  • JavaScript

JavaScript

console.log(_.mean([])); 
You can also try this code with Online Javascript Compiler
Run Code

Output: 

NaN

 

Demonstrates that _.mean() returns NaN when applied to an empty array.

Frequently Asked Questions

How does _.mean() handle non-numeric values in the array?

_.mean() ignores non-numeric values when calculating the mean. However, the presence of such values may affect the calculation, leading to unexpected results.

Can _.mean() be used with large data sets?

Yes, _.mean() can be used with large data sets. However, for extremely large arrays, consider performance implications as the entire array needs to be iterated over.

Is _.mean() the best choice for all average calculations?

_.mean() is suitable for basic average calculations. For weighted averages or other complex statistical computations, specialized libraries or custom functions may be more appropriate.

Conclusion

Lodash's _.mean() method offers a straightforward and efficient way to calculate the average of numerical values in an array. It simplifies and enhances the readability of code involving statistical calculations and data analysis.

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