I often do the `.reduce((xs, ys) => xs.concat(ys))` and I feel the need to leave a comment explaining what it does.
+1 to this proposal. On Fri, May 19, 2017 at 12:44 PM, Luan Nico <[email protected] > wrote: > I always surprise myself finding out there's no native way to flat an > array. I know it's easy to implement, but to increase legibility I propose > creating one of two options (either suffice, I believe): > > - `flatMap` : regular map, but flattens the array afterwards > - `flatten` : just call on an array and get a new flattened array > > Some examples usages of both: > > With `flatMap`: > ``` > const numbers = [12, 35]; > const divisors = numbers.flatMap(factors); > ``` > > With `flatten`: > ``` > const numbers = [12, 35]; > const divisors = numbers.map(factors).flatten(); > ``` > > Flattening an array is converting something like `[1, [2, 3], 4]` to `[1, > 2, 3, 4]`. > There is also need to choose whether it's deep or shallow. > > I suggest adding a `flatten` shallow method, as that would be useful in > all scenarios described. > > Example implementation: > > ``` > Array.prototype.flatten = function () { > return [].concat.apply([], this); > }; > ``` > > What are your opinions about it? > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

