Can someone give a quick overview of the benefits of flatMap? Im still struggling to get my head round a useful use-case.
flatMap
Let's say you have a bunch of objects, let's say they are People{}, and they have an array of children[].
If you wanted the list of children, of an array of people, you could use flat map.
people.flatMap(p => p.children)
So you return the array of children, and all those arrays get flattened into 1 giant array.
Ahh I see, so it’s like .map().flat()?
.map().flat()
Makes sense, still only seeing limited use cases. But saves looping twice I guess.
Might be dependant upon your projects you do. We deal with a lot of nested dynamic data at work, we use this technique a lot.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Can someone give a quick overview of the benefits of
flatMap
? Im still struggling to get my head round a useful use-case.Let's say you have a bunch of objects, let's say they are People{}, and they have an array of children[].
If you wanted the list of children, of an array of people, you could use flat map.
people.flatMap(p => p.children)
So you return the array of children, and all those arrays get flattened into 1 giant array.
Ahh I see, so it’s like
.map().flat()
?Makes sense, still only seeing limited use cases. But saves looping twice I guess.
Might be dependant upon your projects you do. We deal with a lot of nested dynamic data at work, we use this technique a lot.