Skip to content

Function.maxBy

github-actions[bot] edited this page Nov 18, 2026 · 13 revisions

umt / maxBy

Function: maxBy()

maxBy<T, K>(array, selector): T | undefined

Defined in: Array/maxBy.ts:10

Returns the element whose selected value is the largest Values are compared with >, so the first element wins ties and a key that never compares as larger (such as NaN) never replaces the current maximum.

Type Parameters

T

T

K

K

Parameters

array

T[]

The array to scan

selector

(value) => K

Function returning the value to compare

Returns

T | undefined

The element with the largest selected value, or undefined for an empty array

Example

maxBy([{ n: 1 }, { n: 2 }, { n: 2 }], (item) => item.n); // { n: 2 } (the second element)

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally