News for hackers. Stuff that matters.
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)

There are no comments on this comment yet.