es-toolkit/docs/reference/array/minBy.md
Goblin (Kim Woo Seok) 53703f4abb
Some checks are pending
CI / codecov (push) Waiting to run
Release / release (push) Waiting to run
docs: Add detail examples to docs in maxBy, minBy (#554)
* docs: Add detail examples to docs in maxBy, minBy

* docs: Add examples for maxBy and minBy functions to en, ja, zh_hans docs
2024-09-23 18:53:02 +09:00

879 B

minBy

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

Signature

function minBy<T>(items: [T, ...T[]], getValue: (element: T) => number): T;
function minBy<T>(items: T[], getValue: (element: T) => number): T | undefined;

Parameters

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

Returns

(T): 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
minBy(
  [
    { name: 'john', age: 30 },
    { name: 'jane', age: 28 },
    { name: 'joe', age: 26 },
  ],
  x => x.age
); // Returns: { name: 'joe', age: 26 }