Function: filter()
Call Signature
filter<
ElementType,FilteredType>(iterable:IterableResolvable<ElementType>,callbackFn: (element:ElementType,index:number) =>element is FilteredType):IterableIterator<FilteredType>
Defined in: projects/utilities/packages/iterator-utilities/src/lib/filter.ts:21
Creates an iterable with the elements that pass the test implemented by the provided function.
Type Parameters
| Type Parameter |
|---|
ElementType |
FilteredType |
Parameters
| Parameter | Type | Description |
|---|---|---|
iterable | IterableResolvable<ElementType> | The iterator to filter. |
callbackFn | (element: ElementType, index: number) => element is FilteredType | A function to execute for each element produced by the iterator. It should return a truthy value to make the element yielded by the iterator helper, and a falsy value otherwise. |
Returns
IterableIterator<FilteredType>
An iterator that produces elements from the given iterator that satisfy the specified test.
Example
import { filter } from '@sapphire/iterator-utilities';
const iterable = [1, 2, 3, 4, 5];
console.log([...filter(iterable, (value) => value % 2 === 0)]);
// Output: [2, 4]
Call Signature
filter<
ElementType>(iterable:IterableResolvable<ElementType>,callbackFn: (element:ElementType,index:number) =>boolean):IterableIterator<ElementType>
Defined in: projects/utilities/packages/iterator-utilities/src/lib/filter.ts:25
Creates an iterable with the elements that pass the test implemented by the provided function.
Type Parameters
| Type Parameter |
|---|
ElementType |
Parameters
| Parameter | Type | Description |
|---|---|---|
iterable | IterableResolvable<ElementType> | The iterator to filter. |
callbackFn | (element: ElementType, index: number) => boolean | A function to execute for each element produced by the iterator. It should return a truthy value to make the element yielded by the iterator helper, and a falsy value otherwise. |
Returns
IterableIterator<ElementType>
An iterator that produces elements from the given iterator that satisfy the specified test.
Example
import { filter } from '@sapphire/iterator-utilities';
const iterable = [1, 2, 3, 4, 5];
console.log([...filter(iterable, (value) => value % 2 === 0)]);
// Output: [2, 4]