The compact
function from Lodash is used to remove all falsey values from an array. Falsey values in JavaScript are false
, null
, undefined
, 0
, -0
, NaN
, and an empty string (""
).
The syntax for using compact
function in JavaScript is as follows:
index.tsx17 chars2 lines
Here, array
is the array from which you want to remove falsey values.
Example:
index.tsx188 chars8 lines
In the above example, we have used the compact
function from Lodash to remove all the falsey values from the array
and stored the result in the compactArray
. The output of this code is [ 1, 2, 3, 4 ] where all the falsey values (0, false, '', undefined, NaN, and null) are removed from the original array.
gistlibby LogSnag