7 ms·
I have very little experience with arrays in JS, but I always have a nagging feeling that, if my algorithm needs me to flatten an array, then there is something
by Rygian 5y ago
I have very little experience with arrays in JS, but I always have a nagging feeling that, if my algorithm needs me to flatten an array, then there is something wrong with the data structures I am using.
Example in the article is meaningless to me:
array.map(x => [x \* 2]);
// [[2], [4], [6], [8]]
array.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
because the right way would be array.map(x => x *2); anyway.
What am I missing? What is a realistic scenario where an array needs to be flattened?
- thaunatos 5y agoConsider a parent -> child relation. You have an array `parents`, but want all their children. parents.flatMap(parent => parent.children)
- beardedetim 5y agoLet's say I have a list of friend objects: myFriends: { name: string, friends: friend[] }[] If I wanted to get a list of friends of friends, I could do something like myFriends.map(prop('friends')).flatten() Or myFriends.flatMap(prop('friends')) My data is a list of lists but I really want a list of friends so I flatten. Maybe the Friend object is poorly created/designed but often times you'll just have to deal with whatever the API gives you.
- deleted 5y ago[deleted]
- rak1507 5y agoSome sort of function that returns a variable number of arguments. It's basically the same as the list monad in haskell, and there are examples here https://en.wikibooks.org/wiki/Haskell/Understanding_monads/List https://en.wikibooks.org/wiki/Haskell/Understanding_monads/L... that can be followed even if you don't know any Haskell.
- piaste 5y ago> What is a realistic scenario where an array needs to be flattened? Concatenating the result of a paginated API. Showing all the objects two or more 1:N steps away from you in the object graph. The events your friends are attending, the issues your coworkers are working on, the people belonging to any of your same groups. Basically any time you would do a SELECT... JOIN in SQL.
- Spivak 5y agoIf you’re doing that aren’t you kinda defeating the point of pagination? Paginated APIs to me are a contract that says the size of the results can be arbitrarily large and there be dragons if you try to fit them in memory. With how many wrappers around paginated APIs to unpaginate them I must be wrong but it still bugs me.
- piaste 5y agoSometimes the pagination is optional to allow the client to only get as many results as they can handle. But far more often, the pagination is forced - get 100 results, hit this link for the next 100 - in order to limit the load on the server. Most trivial scenario, client runs a search that's too generic, you don't want to waste server resources actually preparing 999999 results.
- nicoburns 5y agoIt occurs commonly any time you are dealing with nested data. For example: at work we deal with "roles" (professions), and underneath those there can be different specialisms. In some of our views we have filters that users can use to show a subset of the data. There is a filter for roles, and a filter for specialisms. But the specialisms filter should only show options that are relevant given the roles selected in the roles filter. So the code for generating the options to display in the specialisms filter dropdown is something like: const availableSpecialisms = roles .filter(role => selectedRoles.includes(role.id)) .flatMap(role => role.specialisms)
- ReleaseCandidat 5y agoWhen `x => [x * 2]` is a function that returns an array, but you do not want an array of arrays. Doesn't make much sense with such a lambda, of course. And of course you could call `flatmap` bind, if you prefer ;)
- saranshk 5y agoadded this to the post as well, a place where there is an array inside an array of objects and you want all those. For example, if we want to extract all roles from the array: [ { name: "Saransh Kataria" , roles: ['system-admin', 'developer'] }, { name: "Wisdom Geek" , roles: ['basic'] }, ].flatMap(x => x.roles); // Output => ["admin", "system-admin", "developer"]
- khalilch2 5y agowouldn't the output be ['basic', 'system-admin', 'developer'] rather than array[0] being 'admin'?
- saranshk 5y agoyou are right, I was trying to format it for HN but somehow screwed up copy pasting, though it'd be ["system-admin", "developer", "basic"] and not ['basic', 'system-admin', 'developer']
- tzs 5y agoYou are right that it should be 'basic', not 'admin'. But the order should be 'system-admin', 'developer', and finally 'basic'.
- deleted 5y ago[deleted]