Skip to content

compact

Import from @varavel/vdl-plugin-sdk/utils/arrays.

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

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

Type Parameters

Type Parameter Description
T The type of elements in the array.

Parameters

Parameter Type Description
arr readonly T[] The input array to remove falsey values.

Returns

Exclude\<T, false | "" | 0 | 0n | null | undefined>[]

  • A new array with all falsey values removed.

Example

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