Function: find()
Call Signature
find<
ElementType,FilteredType>(iterable:IterableResolvable<ElementType>,callbackFn: (element:ElementType,index:number) =>element is FilteredType):undefined|FilteredType
Defined in: projects/utilities/packages/iterator-utilities/src/lib/find.ts:25
Advances the iterable until it finds the element, returning it if it's found and undefined otherwise.
Type Parameters
| Type Parameter |
|---|
ElementType |
FilteredType |
Parameters
| Parameter | Type | Description |
|---|---|---|
iterable | IterableResolvable<ElementType> | An iterator to search for a value in. |
callbackFn | (element: ElementType, index: number) => element is FilteredType | A function that determines if a value is the one being searched for. |
Returns
undefined | FilteredType
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.
Call Signature
find<
ElementType>(iterable:IterableResolvable<ElementType>,callbackFn: (element:ElementType,index:number) =>boolean):undefined|ElementType
Defined in: projects/utilities/packages/iterator-utilities/src/lib/find.ts:29
Advances the iterable until it finds the element, returning it if it's found and undefined otherwise.
Type Parameters
| Type Parameter |
|---|
ElementType |
Parameters
| Parameter | Type | Description |
|---|---|---|
iterable | IterableResolvable<ElementType> | An iterator to search for a value in. |
callbackFn | (element: ElementType, index: number) => boolean | A function that determines if a value is the one being searched for. |
Returns
undefined | ElementType
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.