compact
Removes all falsy values (false, null, undefined, 0, "", NaN) from an array.
null and undefined are treated as empty arrays and return [].
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly Falsy | T[] | null | undefined | The array to compact |
Returns
Section titled “Returns”Exclude<T, Falsy>[] — A new array with only truthy values
Examples
Section titled “Examples”Remove falsy values
Section titled “Remove falsy values”Removes all falsy values (false, null, undefined, 0, "", NaN) from an array.
Filter nullable strings
Section titled “Filter nullable strings”Useful to clean up arrays with null/undefined gaps.
