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?
xxxxxxxxxx
1
const myArray = [1, '', null, NaN, 2, undefined, 3];
2
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:
xxxxxxxxxx
1
const myArray = [1, '', null, NaN, 2, undefined, 3];
2
3
const result = myArray.filter(Boolean);
4
5
console.log(JSON.stringify(result)); // [1,2,3]
See also
0 commentsShow commentsAdd comment