Open In App

Underscore.js _.min Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Underscore.js is a JavaScript library that provides a lot of useful functions that help in programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.min() function is used to find the minimum element from the list passed. If an iterate is given, then it will be applied to each value and generate criteria to rank the values and find the minimum element. 

Syntax:

_.min(list, [iterate], [context])

Parameters: This function accepts three parameters as mentioned above and described below:

  • List: This parameter is used to hold the list of elements.
  • Predicate: This parameter is used to hold the test condition.
  • Context: This parameter is used to display the content.

Return values: The returned value is the element of the list that is minimum. A numbers list will give the least number and that of the string will give the string which is first when they are placed alphabetically. 

Note: If the list is empty then ‘infinity’ will be returned. 

Passing an array of numbers to the _.min function(): The ._min() function takes the element from the list one by one and do compare the elements to find the minimum number in the list. After traversing and comparing all the elements, the _.min() function ends. 

Example: 

html




<html>
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        var numbers = [100, 50, 400, 66, 7900];
        console.log(_.min(numbers));
    </script>
</body>
</html>


Output: 

Passing a list of both numbers and strings as its property to _.min() function: Passing a list of both numbers and strings and comparing the elements by one of the properties. Either by the property of the number or by the string property. Like here comparing the ‘difficulty’ property. The smallest difficulty element will be returned. 

Example: 

html




<html>
    <head>
        <script type="text/javascript" src=
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var languages = [
                    {
                    name: 'HTML',
                    difficulty: 4
                    },
                    {
                    name: 'CSS',
                    difficulty: 5
                    }
                ];
            console.log(_.min(languages, function(o)
            {
                return o.difficulty;
            }));
        </script>
    </body>
</html>


Output: 

Passing a structure of more than 1property to the _.min() function: First declare the array (here array is ‘arr’) and choose one property out of the many, on the basis of which need to find the minimum like here ‘hasLongHairs’. Console.log is the variable in which this returned minimum value is stored. 

Example: 

html




<html>
    <head>
        <script type="text/javascript" src=
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var arr = [
                {prop1:"10", prop2:"07", prop3: "Geeks"},
                {prop1:"12", prop2:"86", prop3: "for"},
                {prop1:"11", prop2:"58", prop3: "Geeks."}
            ];
            console.log(_.min(arr, function(o){return o.prop2;}));
        </script>
    </body>
</html>


Output: 

Passing ‘true’ and ‘false’ as the elements of the list to the _.min() function: Passing the ‘true’ and ‘false’ values to the _.min() function. The minimum of these values will be defined as ‘false’ if it is present in the list at least once otherwise answer will be ‘true’. 

Example: 

html




<html>
    <head>
        <script type="text/javascript" src=
        </script>
    </head>
    <body>
        <script type="text/javascript">
        console.log(_.min([true, false, true]));
        console.log(_.min([true, true]));
        console.log(_.min([false, false]));
        </script>
    </body>
</html>


Output: 



Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads