maxBy
Finds the element that yields the maximum value when the iteratee function is applied to each element of the array.
- If multiple elements share the maximum value, the first one is returned.
- Returns
undefinedif the array is empty. - If the
iterateereturnsNaNfor an element, that element is returned immediately. (like built-inMath.max,NaNtakes precedence) - When a non-empty tuple type (
readonly [T, ...T[]]) is passed, the return type is inferred asT.
Code
Interface
typescript
function maxBy<T>(
items: readonly [T, ...T[]],
iteratee: (element: T, index: number, array: T[] | readonly T[]) => number
): T;
function maxBy<T>(
items: readonly T[],
iteratee: (element: T, index: number, array: T[] | readonly T[]) => number
): T | undefined;
Usage
typescript
import { maxBy } from '@modern-kit/utils';
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], (x) => x.a); // { a: 3 }
maxBy([] as { a: number }[], (x) => x.a); // undefined
maxBy([3, NaN, 1], (x) => x); // NaN
maxBy(
[
{ name: 'john', age: 30 },
{ name: 'jane', age: 28 },
{ name: 'joe', age: 26 },
],
(x) => x.age
); // { name: 'john', age: 30 }