当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

max

优质
小牛编辑
137浏览
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;
  });
}
NameType(s)Description
valueFnFunction?

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'