Skip to main content

Function: indexOf()

indexOf<ElementType>(iterable: IterableResolvable<ElementType>, value: ElementType): 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 a value in.
valueElementTypeThe value to search for.

Returns

number

The index of the first occurrence of the value in the iterator, or -1 if the value is not found.

Example

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

const iterable = [1, 2, 3, 4, 5];
console.log(indexOf(iterable, 3));
// Output: 2

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/indexOf.ts:24