DEV Community

Cover image for Understanding Array.prototype.flatMap

Understanding Array.prototype.flatMap

Laurie on June 25, 2019

Last week we talked about the new flat method available in ES2019. This week, we're going to dive into flatMap! Let's start with map I...
Collapse
 
terabytetiger profile image
Tyler V. (he/him)

Is there a way to pass Infinity into flatmap()? Or would you need to do a Array.flatMap(itme => item).flat(Infinity) roundabout to get that effect? (I'm not even sure if this would be particularly useful in any scenario)

Collapse
 
laurieontech profile image
Laurie

Not that I've seen. I think the way to do it would be the second example you listed.

Collapse
 
tanmayrajani profile image
Tanmay Rajani

If I have more than one operations to do on an array, I subconsciously go to reduce everytime. It's better than remembering these separate functions I think. Not sure if it's the best option complexity-wise..

Collapse
 
laurieontech profile image
Laurie

I think it depends. The Mozilla docs actually mention that for large arrays that option isn’t as efficient.

developer.mozilla.org/en-US/docs/W...

Collapse
 
tanmayrajani profile image
Tanmay Rajani

Makes sense. Thanks for sharing!

 
selbekk profile image
selbekk

Haha totally fair πŸ˜„ i was just trolling. Also, I need to learn what monads are.

Collapse
 
lesha profile image
lesha πŸŸ¨β¬›οΈ

I really hate the fact that libs like lodash are so much more useful than standard library and so happy that some of these features are making their way to standard library.

Hope they add mapValues/mapKeys/pickBy next. Having map/filter equivalents for objects is really convenient

Collapse
 
dukeofetiquette profile image
Adam DuQuette

Code school instructor here, I'd love to see a real-world example in your posts. The simple example is great for the intro, but having a more robust example to solidify might help. I am constantly getting that type of request from students, so I've gotten in the habit of trying to incorporate them during lecture.

That is to say, why are flat() and flatMap() being introduced? Why should a junior developer be excited about these?

Thanks : )

Collapse
 
laurieontech profile image
Laurie

Fair point. Been keeping these intros bite-sized, but I'll certainly consider adding a bit more!

Collapse
 
selbekk profile image
selbekk

You had to go all monad? πŸ˜„

Collapse
 
georgecoldham profile image
George

I have never really understood a use for this. I feel like it is useful, but I just cant find how!

Collapse
 
laurieontech profile image
Laurie

It's a functional programming pattern for sure.

Collapse
 
georgecoldham profile image
George

I love functional programming, its what I push for all my code to be where possible (I often work with other peoples code bases).

I just cant think of a situation where I have got an array that I both need to iterate over and flatten at the same time.

Thread Thread
 
laurieontech profile image
Laurie

Seems like this has some good ones! 2ality.com/2017/04/flatmap.html

Collapse
 
chuckwood profile image
Charles Wood

Isn't flatMap in ES6 (aka ES2015)?

Collapse
 
laurieontech profile image
Laurie

Nope! New in ES2019. Map itself is ES2015.