In order to use the compact
function from the Underscore library in JavaScript, first you need to have Underscore installed and included in your project.
Then, you can use the _.compact()
function to remove all falsy values from an array. This function returns a new array with all falsy values (i.e. null, undefined, NaN, false, 0, and "") removed.
Here's an example:
index.tsx197 chars6 lines
In the code above, newArr
will be a new array with all falsy values removed, so console.log(newArr)
will output [1, 2, 3]
.
gistlibby LogSnag