r/javascript • • Mar 02 '19

Useful javascript one-liners.

https://medium.com/@smellycode/js-one-liners-8bc0a6af2345?source=friends_link&sk=a9295a05aef9e514e137fca7eeb89cd1
344 Upvotes

96 comments sorted by

View all comments

3

u/Danieliverant Mar 02 '19

I would add "group by" using reduce function. I'm on my phone but you can find it on google - a real one line useful function.

2

u/Danieliverant Mar 02 '19

From stackoverflow: (xs, key) => xs.reduce( (rv, x) => (rv[x[key]] = rv[x[key]] || []).push(x) , {});

(I've just converted it to arrow functions - maybe I forgot some brackets)

1

u/smellycode Mar 02 '19

I'll add it asap. Thanks for the suggestion.

-1

u/Danieliverant Mar 02 '19

Just check my syntax before, I've typed it on my phone :)

1

u/smellycode Mar 02 '19

group

Sure :)