EN
JavaScript - filter null values from array
1
answers
0
points
I need to remove false, undefined, null, 0, "" and NaN values from an array.
Anyone can help me with this?
// ONLINE-RUNNER:browser;
const myArray = [1, '', null, NaN, 2, undefined, 3];
const notAllowed = [null, '', false, 0, undefined, NaN];
1 answer
0
points
Use Boolean constructor to filter array from falsy values.
Your code should look like this:
// ONLINE-RUNNER:browser;
const myArray = [1, '', null, NaN, 2, undefined, 3];
const result = myArray.filter(Boolean);
console.log(JSON.stringify(result)); // [1,2,3]
See also
0 comments
Add comment