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)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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?