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
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
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
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 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.