Skip to content

minBy() function

Returns the element of the given Iterable/AsyncIterable for which f returns the smallest value. Returns undefined for an empty iterable. When multiple elements produce the same smallest value, the first one wins.

Signature:

typescript
declare function minBy<A>(f: (a: A) => number, iterable: Iterable<A>): A | undefined;

Example

ts
minBy((a) => a.age, [
  { name: "a", age: 21 },
  { name: "b", age: 41 },
  { name: "c", age: 31 },
]); // { name: "a", age: 21 }

minBy((a) => a.length, []); // undefined

// with pipe
pipe(
  [{ score: 1 }, { score: 3 }, { score: 2 }],
  minBy((a) => a.score),
); // { score: 1 }

Open Source Code

Released under the Apache-2.0 License.