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.
Finding Object with Maximum Property Value:
4.2.
JavaScript
4.3.
Using Property Name Shorthand:
4.4.
Max Value in Array of Arrays:
4.5.
JavaScript
4.6.
Custom Iteratee Function:
4.7.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.maxBy() differ from _.max()?
5.2.
Can _.maxBy() handle non-numeric comparisons?
5.3.
What happens if multiple elements meet the maximum criterion?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.maxBy() Method

Author Gaurav Gandhi
0 upvote

Introduction

In data processing and analysis, finding the maximum value in a collection based on a specific criterion is a common requirement. Lodash's _.maxBy() method addresses this need by allowing you to find the maximum element in an array based on the value returned by an iteratee function. 

Lodash _.maxBy() Method

This is particularly useful when dealing with arrays of objects, where you need to determine the maximum based on a property of the objects.

Why This Function is Used

The _.maxBy() function is used to find the element with the maximum value in an array based on a specific criterion defined by an iteratee. This method is essential when working with collections of complex objects where the maximum needs to be calculated based on a property or computed value, not just on the objects themselves. It simplifies the process of identifying the 'largest' or 'highest' item in a collection according to a defined parameter.

Syntax, Parameter and Return Value

Syntax: 

_.maxBy(array, [iteratee=_.identity])
You can also try this code with Online Javascript Compiler
Run Code

Parameters:

  • array (Array): The array to iterate over.
     
  • [iteratee=_.identity] (Function): The iteratee invoked per element.

Return Value: 

Returns the element with the maximum value.

Examples 

Finding Object with Maximum Property Value:

  • JavaScript

JavaScript

var _ = require('lodash');

var users = [

 { 'user': 'fred', 'age': 48 },

 { 'user': 'barney', 'age': 36 },

 { 'user': 'fred', 'age': 40 }

];

var oldestUser = _.maxBy(users, (o) => o.age);

console.log(oldestUser);
You can also try this code with Online Javascript Compiler
Run Code

Output:

{ 'user': 'fred', 'age': 48 }
You can also try this code with Online Javascript Compiler
Run Code


Demonstrates finding the user with the highest age in an array of user objects.

Using Property Name Shorthand:

var mostExpensiveProduct = _.maxBy(products, 'price');
console.log(mostExpensiveProduct);
You can also try this code with Online Javascript Compiler
Run Code


An example of using property name shorthand to find the most expensive product.

Max Value in Array of Arrays:

  • JavaScript

JavaScript

var points = [[1, 2], [3, 4], [2, 8]];

var highestPoint = _.maxBy(points, (p) => p[1]);

console.log(highestPoint);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[2, 8]


Shows finding the point with the highest y-value in an array of coordinates.

Custom Iteratee Function:

  • JavaScript

JavaScript

var data = [

 { name: 'Item1', dimensions: { width: 10, height: 5 }},

 { name: 'Item2', dimensions: { width: 4, height: 20 }}

];

var largestAreaItem = _.maxBy(data, item => item.dimensions.width * item.dimensions.height);

console.log(largestAreaItem.name);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'Item2'


Uses a custom function to determine the item with the largest area.

Frequently Asked Questions

How does _.maxBy() differ from _.max()?

_.max() finds the maximum numeric value in a simple array, while _.maxBy() is used for finding the maximum value in an array based on a specific criterion or property.

Can _.maxBy() handle non-numeric comparisons?

Yes, _.maxBy() can be used for non-numeric comparisons, as long as the iteratee function returns a value that can be logically ordered (like strings or dates).

What happens if multiple elements meet the maximum criterion?

If multiple elements are considered 'maximum', _.maxBy() returns the first one encountered in the collection.

Conclusion

Lodash's _.maxBy() method is a versatile tool for identifying the maximum element in a collection based on a defined criterion. It is particularly valuable when working with arrays of objects and needing to determine the maximum based on a property or a result of a function.

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