minBy
Returns the element of an array with the smallest derived key, computed by keyFn.
Unlike min, which only compares raw numbers, minBy 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 smallest key, or undefined for empty arrays, null, undefined
Examples
Section titled “Examples”Pick the item with the smallest derived value
Section titled “Pick the item with the smallest derived value”Unlike min(), minBy() returns the whole item, chosen by a key derived from it.
Empty array returns undefined
Section titled “Empty array returns undefined”There is no minimum to return for an empty array.
