DEV Community

Discussion on: 1 line of code: How to clean an Array

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Why not just this?

arr.filter(i=>i)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lexlohr profile image
Alex Lohr

Because it was explicitly stated that false and 0 should not be filtered. Your code would filter them.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Ah, I misread - you want to keep 0 and false. In which case, you can shorten to:

arr.filter(item => item || item === 0 || item === false)
Enter fullscreen mode Exit fullscreen mode

The !! is unnecessary.