Lodash - maxBy 方法
语法
_.maxBy(array, [iteratee=_.identity])
此方法与 _.max 类似,不同之处在于它接受 iteratee,它会针对数组中的每个元素调用 iteratee 来生成值排序的标准。 iteratee 使用一个参数调用:(value)。
参数
array (Array) − 要迭代的数组。
[iteratee=_.identity] (Function) −每个元素调用的迭代器。
输出
(*) − 返回最大值。
示例
var _ = require('lodash'); var values = [{ 'n': 1 }, { 'n': 2 }]; var result = _.maxBy(values, function(o) { return o.n; }); console.log(result); result = _.maxBy(values, 'n'); console.log(result);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
{ n: 2 }