Skip to content

compact

The compact function removes falsy values from an array.

Syntax

typescript
compact<T>(array: T[]): T[];

Parameters

NameTypeDescription
arrayT[]Source array

Returns

TypeDescription
T[]New array without falsy values

Examples

typescript
compact([0, 1, false, 2, '', 3]); // => [1, 2, 3]
compact([null, undefined, NaN, 4]); // => [4]

Notes

  • Falsy values include: false, 0, "", null, undefined, NaN.
  • Does not modify the original array.

References

Released under the MIT License.