es-toolkit/docs/reference/array/minBy.md
ethan 6f60b786d5
docs(minBy): Fix example in minBy (#141)
change the returns : { a : 3 } to { a : 1 }
2024-07-09 20:37:00 +09:00

665 B

minBy

Finds the element in an array that has the minimum value when applying the getValue function to each element.

If the list is empty, returns undefined.

Signature

function minBy<T>(items: T[], getValue: (item: T) => number): T;

Parameters

  • items (T[]): The array of elements to search.
  • getValue ((item: T) => number): A function that selects a numeric value from each element.

Returns

The element with the minimum value as determined by the getValue function.

Example

minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
minBy([], x => x.a); // Returns: undefined