
2.15 max
2.15.1 语法:
_.max(list, [iteratee], [context])
2.15.2 说明:
返回list中的最小值。
- list为集合,数组、对象、字符串或arguments
- iteratee作为返回最大值的依据
- iteratee的参数(value, key, list)
- context可以改变iteratee内部的this
2.15.3 代码示例:
示例一:从不同的集合中取出最大值
_.max([1, 2, 3]); //=> 3
_.max({a:1, b:2, c:3}); //=> 3
_.max('123'); //=> '3'
示例二:iteratee作为返回最大值的依据
var max = _.max([1, 2, 3], function(n){
return -n; //-1最大,作为最大值的依据。
});
console.log(max); //=> 1
示例三:iteratee可以为list元素的属性
var max = _.max(['aaa', 'bb', 'c'], 'length');
console.log(max); //=> 'aaa'
示例四:iteratee可以为list元素的key
var arr = [{name: 'iro', age : 15}, {name: 'moe', age : 20}, {name: 'kyo', age : 18}]
var max = _.max(arr, 'age');
console.log(max); //=> Object {name: "moe", age: 20}
示例五:context可以改变iteratee内部的this
var max = _.max([1, 2], function (n) {
console.log(this); //=> Object {no: 5}
return this.no - n;
}, {no : 5});
2.15.4 list的特殊情况
_.max(null); //=> -Infinity
_.max(undefined); //=> -Infinity
_.max(null, undefined); //=> -Infinity
_.max(Infinity); //=> -Infinity
_.max(true); //=> -Infinity
_.max(false); //=> -Infinity
_.max([]); //=> -Infinity
_.max({}); //=> -Infinity
_.max(1); //=> -Infinity
_.max({'a': 'a'}); //=> -Infinity
_.max(1, 'abc'); //=> -Infinity