Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 Usage to Find Minimum Value:
4.2.
JavaScript
4.3.
Handling Empty Array:
4.4.
JavaScript
4.5.
Application in Data Analysis:
4.6.
JavaScript
4.7.
Comparing with Native JavaScript Methods:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
Does _.min() work with non-numeric values?
5.2.
How does _.min() handle NaN or undefined values in the array?
5.3.
Can _.min() be used with arrays of objects?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.min() Method

Author Rinki Deka
0 upvote

Introduction

Identifying the smallest value in a collection is a common requirement in data processing and analysis. Lodash, a widely-used JavaScript utility library, simplifies this task with the _.min() method. 

Lodash _.min() Method

This method efficiently finds the smallest number in an array, making it invaluable for tasks that involve numerical comparisons and analysis.

Why This Function is Used

The _.min() function is used to find the smallest number in an array of numbers. It provides a more readable and convenient way to determine the minimum value, particularly useful in statistical calculations, optimizations, or during any operation that requires identifying the lowest value in a set of numerical data.

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: 

_.min(array)

Parameters:

array (Array): The array to iterate over.

Return Value: 

(Number) - Returns the minimum value found in the array. If the array is empty or falsey, _.min() returns undefined.

Examples 

Basic Usage to Find Minimum Value:

  • JavaScript

JavaScript

var _ = require('lodash');

console.log(_.min([4, 2, 8, 6]));

Output: 

2


Demonstrates finding the smallest number in a simple array.

Handling Empty Array:

  • JavaScript

JavaScript

console.log(_.min([])); 

Output:

undefined


Shows that _.min() returns undefined when applied to an empty array.

Application in Data Analysis:

  • JavaScript

JavaScript

var temperatures = [76, 85, 90, 68, 88];

var lowestTemperature = _.min(temperatures);

console.log('Lowest Temperature:', lowestTemperature);

Output:

'Lowest Temperature: 68'


An example of using _.min() to find the lowest temperature in a collection of data.

Comparing with Native JavaScript Methods:

  • JavaScript

JavaScript

var numbers = [10, 21, 3, 15];

var minNumber = Math.min(...numbers);

console.log(minNumber);

Output: 

3


Demonstrates achieving a similar result with native JavaScript's Math.min() for comparison.

Frequently Asked Questions

Does _.min() work with non-numeric values?

_.min() is designed for arrays of numbers. Non-numeric values in the array are ignored in the calculation, and may lead to undefined if no numeric values are present.

How does _.min() handle NaN or undefined values in the array?

NaN or undefined values in the array are ignored by _.min(), and they do not affect the calculation of the minimum value.

Can _.min() be used with arrays of objects?

To find the minimum value in an array of objects based on a specific property, you should use _.minBy() instead, which allows specifying an iteratee for comparison.

Conclusion

Lodash's _.min() method provides a simple and efficient way to find the minimum value in an array of numbers. It enhances code readability and is especially useful in comparison to more verbose native JavaScript approaches in certain scenarios.

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