maxBy
Returns the element of an array with the greatest derived key, computed by keyFn.
Unlike max, which only compares raw numbers, maxBy returns the item itself,
not the key. On a tie, the earliest element with that key wins.
null and undefined are treated as empty arrays and return undefined.
Available since v3.0.8
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | null | undefined | Array of items |
keyFn | function | Derives the comparable key for an item |
Returns
Section titled “Returns”T | undefined — The element with the greatest key, or undefined for empty arrays, null, undefined
Examples
Section titled “Examples”Pick the item with the greatest derived value
Section titled “Pick the item with the greatest derived value”Unlike max(), maxBy() returns the whole item, chosen by a key derived from it.
Empty array returns undefined
Section titled “Empty array returns undefined”There is no maximum to return for an empty array.
