1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Parameters:
3.2.
Return Value:
4.
Examples
4.1.
Finding Object with Minimum Property Value:
4.2.
JavaScript
4.3.
Using Property Name Shorthand:
4.4.
JavaScript
4.5.
Minimum Value in Array of Arrays:
4.6.
JavaScript
4.7.
Custom Iteratee Function:
4.8.
JavaScript
5.
5.1.
How does _.minBy() differ from _.min()?
5.2.
Can _.minBy() handle non-numeric comparisons?
5.3.
What happens if multiple elements meet the minimum criterion?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Lodash _.minBy() Method

Gaurav Gandhi
0 upvote

## Introduction

Finding the minimum value in a collection based on a specific criterion is a common task in programming, particularly in data analysis and processing. Lodash's _.minBy() method simplifies this process by allowing you to find the minimum element in an array based on the value returned by an iteratee function. T

This method is highly useful when dealing with arrays of objects and you need to determine the minimum based on a property of these objects.

## Why This Function is Used

The _.minBy() function is used to find the element with the minimum value in an array based on a specific criterion defined by an iteratee. This is essential for working with collections of objects where you need to calculate the minimum based on a property or a computed value, rather than on the objects themselves. It streamlines the process of finding the 'smallest' or 'lowest' item in a collection according to a defined parameter.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

• JavaScript

### 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); ``

Output:

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

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

• JavaScript

### 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);``

Output

``{ name: 'Product 1', price: 100 }``

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

• JavaScript

### JavaScript

``var points = [[1, 2], [3, 4], [2, 1]];var lowestPoint = _.minBy(points, (p) => p[1]);console.log(lowestPoint); ``

Output:

``[2, 1]``

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

• JavaScript

### 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); ``

Output:

``'Smallest Item: Item1'``

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

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