Skip to main content

find

find() function

Looks through each value in Iterable/AsyncIterable, returning the first one that passes a truth test f, or undefined if no value passes the test.

Signature:
declare function find<T>(f: (a: T) => unknown, iterable: Iterable<T>): T | undefined;

declare function find<T>(f: (a: T) => unknown, iterable: AsyncIterable<T>): Promise<T | undefined>;

declare function find<T extends Iterable<unknown> | AsyncIterable<unknown>>(f: (a: IterableInfer<T>) => unknown, iterable?: T): (iterable: T) => ReturnValueType<T, IterableInfer<T> | undefined>;

Example

find((a) => a === 2, [1, 2, 3, 4]); // 2

find((a) => a === "r", "marpple"); // 'r'

Try It

Open Source Code