Well, since all of your values are falsy, just do a !!
(cast to boolean) check:
[1,"", null, NaN, 2, undefined,4,5,6].filter(x => !!x); //returns [1, 2, 4, 5, 6]
Edit: Apparently the cast isn't needed:
[1,"", null, NaN, 2, undefined,4,5,6].filter(x => x);
And the code above removes null
just fine, it's NaN
that's the problem. NaN !== NaN
as Nina says in her answer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…