Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.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 _.min() function.
Syntax:
_.minBy( array, [iteratee = _.identity] )
Parameters: This method accepts two parameters as mentioned above and described below:
- array: It is the array that the method iterates over to get the minimum element.
- iteratee: It is the function that is invoked for every element in the array.
Return Value: This method returns the minimum element.
Example 1:
Javascript
const _ = require( "lodash" );
var arr = [{ 'n' : 4 }, { 'n' : 2 }, { 'n' : 6 }];
let min_val =
_.minBy(arr, function (o) { return o.n; });
console.log(min_val);
|
Output:
{ 'n': 2 }
Example 2:
Javascript
const _ = require( "lodash" );
var arr = [{ 'n' : 10 }, { 'n' : 5 },
{ 'n' : 3 }, { 'n' : 12 }];
let min_val = _.minBy(arr, 'n' );
console.log(min_val);
|
Output:
{ 'n': 3 }