Search code snippets, questions, articles...

Remove all false, null, undefined and empty values from array using Lodash

To remove all falsey values like - false, null, 0, "", undefined and NaN, you can use lodash _.compact() method.
const arr = [5, 'one', 0, 1, false, 2, NaN, '', 3, undefined];
const new_arr = _.compact(arr)

The _.compact() method takes the array as parameter and return new array that does not contains falsey values.

Live Demo

Search Index Data (The code snippet can also be found with below search text)

_.compact lodash
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet