Skip to main content

Function: filter()

filter(iterable, callbackFn)

filter<ElementType, FilteredType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => element is FilteredType): IterableIterator<FilteredType>

Creates an iterable with the elements that pass the test implemented by the provided function.

Type Parameters

Type Parameter
ElementType
FilteredType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to filter.
callbackFn(element: ElementType, index: number) => element is FilteredTypeA 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]

Defined in

projects/utilities/packages/iterator-utilities/src/lib/filter.ts:21

filter(iterable, callbackFn)

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

Type Parameters

Type Parameter
ElementType

Parameters

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

Returns

IterableIterator<ElementType>

Defined in

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