Skip to main content

Function: find()

find(iterable, callbackFn)

find<ElementType, FilteredType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => element is FilteredType): FilteredType | undefined

Advances the iterable until it finds the element, returning it if it's found and undefined otherwise.

Type Parameters

Type Parameter
ElementType
FilteredType

Parameters

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

Returns

FilteredType | undefined

Example

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

const iterable = [1, 2, 3, 4, 5];
console.log(find(iterable, (value) => value % 2 === 0));
// 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/find.ts:25

find(iterable, callbackFn)

find<ElementType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => boolean): ElementType | undefined

Type Parameters

Type Parameter
ElementType

Parameters

ParameterType
iterableIterableResolvable<ElementType>
callbackFn(element: ElementType, index: number) => boolean

Returns

ElementType | undefined

Defined in

projects/utilities/packages/iterator-utilities/src/lib/find.ts:29