javascript

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

Was this helpful?