Lodash _.min() 方法
Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
_.min()方法用于从数组中找到最小元素。如果数组为空,则返回 undefined。
句法:
_.min( array )
参数:此方法接受如上所述和如下所述的单个参数:
- 数组:它是该方法迭代以获得最小元素的数组。
返回值:此方法返回数组中的最小元素。
示例 1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.min() method
let min_val = _.min([]);
// Printing the output
console.log(min_val);
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.min() method
let min_val = _.min([15, 7, 38, 46, 82]);
// Printing the output
console.log(min_val);
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.min() method
let min_val = _.min([-15, 7, 38, -46, -82]);
// Printing the output
console.log(min_val);
输出:
undefined
示例 2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.min() method
let min_val = _.min([15, 7, 38, 46, 82]);
// Printing the output
console.log(min_val);
输出:
7
示例 3:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.min() method
let min_val = _.min([-15, 7, 38, -46, -82]);
// Printing the output
console.log(min_val);
输出:
-82