DEV Community

Discussion on: The future of Javascript - features to keep an eye on

Collapse
 
georgecoldham profile image
George

Can someone give a quick overview of the benefits of flatMap? Im still struggling to get my head round a useful use-case.

Collapse
 
wormss profile image
WORMSS • Edited

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.

Collapse
 
georgecoldham profile image
George

Ahh I see, so it’s like .map().flat()?

Makes sense, still only seeing limited use cases. But saves looping twice I guess.

Thread Thread
 
wormss profile image
WORMSS

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.