Open In App

Lodash _.minBy() Method

Lodash _.minBy() method is used to compute the minimum value from the original array by iterating over each element in the array using the Iteratee function. It is almost the same as the _.min() function.

Syntax:



_.minBy(array, [iteratee = _.identity]);

Parameters:

Return Value:

This method returns the minimum element.

Example 1: In this example, we are printing the minimum value of the given array by the use of the _.minBy() method.






// Requiring the lodash library 
const _ = require("lodash");
 
// Original array
let arr = [{ 'n': 4 }, { 'n': 2 }, { 'n': 6 }];
 
// Use of _.minBy() method
let min_val =
    _.minBy(arr, function (o) { return o.n; });
 
// Printing the output 
console.log(min_val);

Output:

{ 'n': 2 }

Example 2: In this example, we are printing the minimum value of the given array by the use of the _.minBy() method.




// Requiring the lodash library 
const _ = require("lodash");
 
// Original array
let arr = [{ 'n': 10 }, { 'n': 5 },
{ 'n': 3 }, { 'n': 12 }];
 
// Use of _.minBy() method
let min_val = _.minBy(arr, 'n');
 
// Printing the output 
console.log(min_val);

Output:

{ 'n': 3 }
Article Tags :