Skip to main content

Function: findIndex()

findIndex<ElementType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => boolean): number

Advances the iterable until it finds the element, returning its index if it's found and -1 otherwise.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to search for an element in.
callbackFn(element: ElementType, index: number) => booleanA function that determines if an element is the one being searched for.

Returns

number

The index of the first element that satisfies the predicate, or -1 if no elements satisfy the predicate.

Example

import { findIndex } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
console.log(findIndex(iterable, (value) => value % 2 === 0));
// Output: 1

Remarks

This function consumes the iterator until the value is found or the iterator is exhausted.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/findIndex.ts:25