Finds an element by a predicate function within the given array, otherwise, it returns the given fallback value or undefined when fallback is not present.

Type signature

<T>(predicate: (value: T, index: number, context: T[]) => boolean, fallback?: T) => (xs: T[]) => T

Examples

find((x) => x > 2)([1, 2, 3, 5, 7]);
// ⇒ 3
Try in REPL
find((x) => x > 2)([
  1, 2, -3, -5, -7,
]);
// ⇒ undefined
Try in REPL

Questions

  • How to find an element of an array by a given predicate?

TypeScript sourceJavaScript source