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...
For further actions, you may consider blocking this person and/or reporting abuse
Is there a way to pass
Infinity
intoflatmap()
? Or would you need to do aArray.flatMap(itme => item).flat(Infinity)
roundabout to get that effect? (I'm not even sure if this would be particularly useful in any scenario)Not that I've seen. I think the way to do it would be the second example you listed.
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..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...
Makes sense. Thanks for sharing!
Haha totally fair π i was just trolling. Also, I need to learn what monads are.
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
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 : )
Fair point. Been keeping these intros bite-sized, but I'll certainly consider adding a bit more!
You had to go all monad? π
I have never really understood a use for this. I feel like it is useful, but I just cant find how!
It's a functional programming pattern for sure.
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.
Seems like this has some good ones! 2ality.com/2017/04/flatmap.html
Isn't flatMap in ES6 (aka ES2015)?
Nope! New in ES2019. Map itself is ES2015.