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]

Source

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>

Source

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