Skip to main content

Function: compact()

compact<ElementType>(iterable: IterableResolvable<undefined | null | ElementType>): IterableIterator<ElementType>

Creates a new iterable that yields all the non-nullish values (null and undefined) from the iterable.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<undefined | null | ElementType>An iterator that contains elements to be compacted.

Returns

IterableIterator<ElementType>

Example

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

const iterable = [1, null, 2, undefined, 3];
console.log([...compact(iterable)]);
// Output: [1, 2, 3]

Defined in

projects/utilities/packages/iterator-utilities/src/lib/compact.ts:18