Skip to content

max ​

INFO

出于兼容性原因,此函数仅在 es-toolkit/compat 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。

从 es-toolkit/compat 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 这里。

找到数组中具有最大值的元素。

如果列表为空,则返回 undefined。

签名 ​

typescript
function max<T>(items: [T, ...T[]]): T;
function max(): undefined;
function max<T>(items?: T[]): T | undefined;

参数 ​

  • items (T[]): 要搜索的元素数组。

返回 ​

(T): 具有最大值的元素。

示例 ​

typescript
max([1, 2, 3]); // Returns: 3
max(['a', 'b']); // Returns: 'b'