News for hackers. Stuff that matters.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by Rygian on Wednesday January 05, 2022 @11:05AM (#29810659)
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?

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by piaste on Wednesday January 05, 2022 @11:14AM (#29810825)
> 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.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by Spivak on Wednesday January 05, 2022 @11:57AM (#29811493)
If 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.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by piaste on Wednesday January 05, 2022 @02:30PM (#29813717)
Sometimes 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.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by nicoburns on Wednesday January 05, 2022 @11:15AM (#29810830)
It 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)
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by thaunatos on Wednesday January 05, 2022 @11:09AM (#29810727)
Consider a parent -> child relation. You have an array `parents`, but want all their children.

    parents.flatMap(parent => parent.children)
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by rak1507 on Wednesday January 05, 2022 @11:13AM (#29810794)
Some 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/L... that can be followed even if you don't know any Haskell.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by beardedetim on Wednesday January 05, 2022 @11:10AM (#29810745)
Let'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.

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by ReleaseCandidat on Wednesday January 05, 2022 @11:15AM (#29810839)
When `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 ;)

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by saranshk on Wednesday January 05, 2022 @11:35AM (#29811158)
added 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"]

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by khalilch2 on Wednesday January 05, 2022 @12:47PM (#29812287)
wouldn't the output be ['basic', 'system-admin', 'developer'] rather than array[0] being 'admin'?
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by tzs on Wednesday January 05, 2022 @01:37PM (#29813069)
You are right that it should be 'basic', not 'admin'. But the order should be 'system-admin', 'developer', and finally 'basic'.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by saranshk on Wednesday January 05, 2022 @01:39PM (#29813093)
you 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']