-2

I would like to keep 0 and need to remove undefined, null '' ,, from the array. I tried this:

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(function (el) {
  return el !== null
});

console.log(filtered);

But getting result as : [0, 1, 2, '', 3, undefined, 3, 4, 4, 5, 6]

if I add the condition as return el != null, still empty space exist and getting error from lint. how to handle this?

thanks in advance

0stone0
  • 34,288
  • 4
  • 39
  • 64
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

1 Answers1

4

You can use return e || e === 0 to filter it

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(e =>{ 
  return e || e === 0
})

console.log(filtered);
flyingfox
  • 13,414
  • 3
  • 24
  • 39