maxBy source npm

_.maxBy(array, [iteratee=_.identity])

这个方法类似 _.max 除了它接受 iteratee 调用每一个元素,根据返回的 value 决定排序准则。 iteratee 会传入1个参数:(value)。

参数

  1. array (Array)

    要遍历的数组

  2. [iteratee=_.identity] (Function|Object|string)

    这个函数会处理每一个元素

返回值 (*)

返回最大值

示例

var objects = [{ 'n': 1 }, { 'n': 2 }];

_.maxBy(objects, function(o) { return o.n; });
// => { 'n': 2 }

// 使用了 `_.property` iteratee 的回调结果
_.maxBy(objects, 'n');
// => { 'n': 2 }