Skip to main content

findIndex

findIndex() function

Returns the index of the first element of Iterable/AsyncIterable which matches f, or -1 if no element matches.

Signature:
declare function findIndex<T extends readonly []>(f: Arrow, iterable: T): -1;

declare function findIndex<T>(f: (a: T) => unknown, iterable: Iterable<T>): number;

declare function findIndex<T>(f: (a: T) => unknown, iterable: AsyncIterable<T>): Promise<number>;

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

Example

const arr = [{a:1}, {a:2}, {a:3}]
findIndex((obj) => obj.a === 1, arr); // 0
findIndex((obj) => obj.a === 2, arr); // 1
findIndex((obj) => obj.a === 4, arr); // -1

Open Source Code