max
优质
小牛编辑
144浏览
2023-12-01
Gets the maximum value in the sequence.
Signature
Sequence.max = function(valueFn) { /*...*/ }
Sequence.max = function max(valueFn) { if (typeof valueFn !== "undefined") { return this.maxBy(valueFn); } return this.reduce(function(prev, current, i) { if (typeof prev === "undefined") { return current; } return current > prev ? current : prev; }); }
Name | Type(s) | Description |
---|---|---|
valueFn | Function? | The function by which the value for comparison is calculated for each element in the sequence. |
returns | * | The element with the highest value in the sequence, or undefined if the sequence is empty. |
Examples
function reverseDigits(x) { return Number(String(x).split('').reverse().join('')); } Lazy([]).max() // => undefined Lazy([1]).max() // => 1 Lazy([1, 2]).max() // => 2 Lazy([2, 1]).max() // => 2 Lazy([6, 18, 2, 48, 29]).max() // => 48 Lazy([6, 18, 2, 48, 29]).max(reverseDigits) // => 29 Lazy(['b', 'c', 'a']).max() // => 'c'