DEV Community

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

Collapse
 
damjand profile image
Damjan Dimitrov

Can be achieved with a very short statement: arr.filter(Boolean).

The comment by @lexlohr should also be regarded if we don't want to filter out values such as 0 or false.

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Shorter than arr.filter(Boolean) is arr.filter(i=>i)

Collapse
 
martinkr profile image
Martin Krause

Hi Damjan,

thank you for the suggestion. Indeed, this function should preserve 0 and false. Array.filter(Boolean) will be another oneliner which removes everything that is considered "false".
Cheers!