Lodash是一个JavaScript库,可在underscore.js顶部使用。 Lodash帮助处理数组,字符串,对象,数字等。
_.maxBy()方法用于通过使用Iteratee函数遍历数组中的每个元素来从原始数组计算最大值。它与_.max()功能几乎相同。
用法:
_.maxBy( array, [iteratee = _.identity] )
参数:该方法接受上述和以下所述的两个参数:
- array:该方法迭代以获得最大元素的数组。
- iteratee:它是为数组中的每个元素调用的函数。
返回值:此方法返回最大元素。
范例1:
Java脚本
// Requiring the lodash library
const _ = require("lodash");
// Original array
var arr = [{ 'n':4 }, { 'n':2 }, { 'n':6 }];
// Use of _.maxBy() method
let max_obj = _.maxBy(arr, function(o) { return o.n; });
// Printing the output
console.log(max_obj);
输出:
{ 'n':6 }
范例2:
Java脚本
// Requiring the lodash library
const _ = require("lodash");
// Original array
var arr = [{ 'n':10 }, { 'n':5 },
{ 'n':3 }, { 'n':12 }];
// Use of _.maxBy() method
let max_obj = _.maxBy(arr, 'n');
// Printing the output
console.log(max_obj);
输出:
{ 'n':12 }
相关用法
- Lodash _.take()用法及代码示例
- Lodash _.zip()用法及代码示例
- Lodash _.add()用法及代码示例
- Lodash _.nth()用法及代码示例
- Lodash _.last()用法及代码示例
- Lodash _.every()用法及代码示例
- Lodash _.max()用法及代码示例
- Lodash _.min()用法及代码示例
- Lodash _.xor()用法及代码示例
- Lodash _.unionWith()用法及代码示例
- Lodash _.unionBy()用法及代码示例
- Lodash _.remove()用法及代码示例
- Lodash _.unzip()用法及代码示例
- Lodash _.union()用法及代码示例
- Lodash _.pullAt()用法及代码示例
- Lodash _.pullAll()用法及代码示例
- Lodash _.pull()用法及代码示例
- Lodash _.minBy()用法及代码示例
- Lodash _.fromPairs()用法及代码示例
- Lodash _.sortedLastIndex()用法及代码示例
注:本文由纯净天空筛选整理自sanjoy_62大神的英文原创作品 Lodash _.maxBy() Method。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。