DEV Community

Discussion on: JavaScript Array Methods - Filtering

Collapse
 
orimdominic profile image
Orim Dominic Adah

Nice. Check this out

const withUndefined = [
'this', 'is', 'an', 'array', 
'of', undefined, 'strings'
]
const withoutUndefined = withUndefined.filter((str) => str)
Enter fullscreen mode Exit fullscreen mode

The idea is that the return value of the callback is truthy or falsy

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Yep. Using an identity function like that with filter will remove anything 'falsy' from the array:

arr.filter(i => i)
Enter fullscreen mode Exit fullscreen mode

Also, you don't need the parentheses around the first str in your example

Thread Thread
 
orimdominic profile image
Orim Dominic Adah • Edited

Sure. I wrote this and copied it from a linted editor, that's why.
I also think it is more readable with the parentheses
Thanks