Syntax, Parameter and Return Value
Syntax: _.minBy(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 minimum is calculated.
Return Value:
Returns the element with the minimum value.
Examples
Finding Object with Minimum Property Value:
JavaScript
var _ = require('lodash');
var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
var youngestUser = _.minBy(users, (o) => o.age);
console.log(youngestUser);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'user': 'barney', 'age': 36 }
Demonstrates finding the user with the lowest age in an array of user objects.
Using Property Name Shorthand:
JavaScript
var products = [
{ name: 'Product 1', price: 100 },
{ name: 'Product 2', price: 200 },
{ name: 'Product 3', price: 150 }
];
var cheapestProduct = _.minBy(products, 'price');
console.log(cheapestProduct);

You can also try this code with Online Javascript Compiler
Run Code
Output
{ name: 'Product 1', price: 100 }
An example of using property name shorthand to find the cheapest product.
Minimum Value in Array of Arrays:
JavaScript
var points = [[1, 2], [3, 4], [2, 1]];
var lowestPoint = _.minBy(points, (p) => p[1]);
console.log(lowestPoint);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[2, 1]
Shows finding the point with the lowest 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 smallestItem = _.minBy(data, item => item.dimensions.width * item.dimensions.height);
console.log('Smallest Item:', smallestItem.name);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Smallest Item: Item1'
Uses a custom function to determine the item with the smallest area.
Frequently Asked Questions
How does _.minBy() differ from _.min()?
_.min() finds the minimum numeric value in a simple array, while _.minBy() is used for finding the minimum value in an array based on a specific criterion or property.
Can _.minBy() handle non-numeric comparisons?
Yes, _.minBy() 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 minimum criterion?
If multiple elements are considered 'minimum', _.minBy() returns the first one encountered in the collection.
Conclusion
Lodash's _.minBy() method is a versatile tool for identifying the minimum element in a collection based on a defined criterion. It is particularly valuable when working with arrays of objects and needing to determine the minimum 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.