es-toolkit/docs/reference/array/compact.md
Dongho Kim 2e1f1177ed
fix(compact): Update NotFalsey to exclude 0n (#588)
* fix(compact): Update `NotFalsey` to exclude `0n`

* docs(compact): Update docs formatting
2024-09-24 10:19:48 +09:00

547 B

compact

Removes falsey values (false, null, 0, 0n, '', undefined, NaN) from an array.

Signature

function compact<T>(arr: T[]): Array<Exclude<T, false | null | 0 | 0n | '' | undefined>>;

Parameters

  • arr (T[]): The input array to remove falsey values.

Returns

(Array<Exclude<T, false | null | 0 | 0n | '' | undefined>>) A new array with all falsey values removed.

Examples

compact([0, 0n, 1, false, 2, '', 3, null, undefined, 4, NaN, 5]);
// Returns: [1, 2, 3, 4, 5]