Lodash - maxBy 方法



語法

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

此方法類似於 _.max,但它接受 iteratee,該 iteratee 對陣列中的每一個元素進行呼叫,以生成 criterion,值按此進行排序。使用一個引數呼叫 iteratee:(value)。

引數

  • array (陣列) − 要迭代的陣列。

  • [iteratee=_.identity] (函式) − 每元素呼叫的 iteratee。

輸出

  • (*) − 返回最大值。

示例

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 }
lodash_math.htm
廣告
© . All rights reserved.