0

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

0

1 Answer 1

5

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.