DEV Community

My New Friends filter() and map()

SavagePixie on July 24, 2019

Today I have been learning about arrow functions. I've got to say that at first it's hard to wrap my head around the idea that a function can take ...
Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’ β€’ Edited

.some and .every are amazing too, also .reduce. All the functors rock. Also Array.from

This is a nifty trick too, pass a Boolean construtor to clean an array of all falsely or holes, return only truthy values.

["", 0, "HEY", , null, -Infinity]
    .filter(Boolean) // => ["Hey"]

Lastly .find is just like filter but returns one value rather than an array.

Ps.
I think all the functors are based on reduce.

PSS.
This is functional programming where this expression is preferred over statements like if and for. You will be surprised how much you can get away with.

Collapse
Β 
savagepixie profile image
SavagePixie β€’

Ooh! New toys! I'll have to check them and see what they do.

Collapse
Β 
bananabrann profile image
Lee β€’

Incredible how there's still things to learn. I will definitely start looking at ways to through in this nifty trick, and that .find too! Thanks!

Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Looks like I should do a post on JavaScript arrays

Collapse
Β 
jeresuikkila profile image
Jere Suikkila β€’

They're amazing. Not a day goes by without me writing one of these! I love how easy they are to write and how understandable they are to read afterwards to know what a piece of code does.

Collapse
Β 
jacoby profile image
Dave Jacoby β€’

I was about to say that, if you like map and filter, you'll love reduce, but it's been said...

Collapse
Β 
savagepixie profile image
SavagePixie β€’

I've looked at it a bit, but I think it'll be tomorrow's trouble, hehe

Collapse
Β 
jacoby profile image
Dave Jacoby β€’

It'll be there when you get to it.

In my language of choice, Perl, map exists, filter is grep, and reduce comes from a non-standard library. It still fills me with joy.

Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Groovy well that's straightend out. πŸ˜…

Collapse
Β 
webdeasy profile image
webdeasy.de β€’

Good explanation and examples! 😊

Collapse
Β 
praneetnadkar profile image
Praneet Nadkar β€’

The explanation was neat!

Collapse
Β 
yashwanthambati profile image
Yashwanth β€’

These are my enemies from today onwards.. haha