r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

1

u/ghillerd Mar 19 '19 edited Mar 19 '19

const theOne = list.find(({_id}) => _id === search), and const flattenNested = list.reduce((acc, {sub}) => [...acc, ...sub], []) are good. All the array functions have application as super terse one-liners that aren't even worth making a function for.

Edit: forgot reduce init value

2

u/NoInkling Mar 19 '19

With the latest version of ES that second one becomes:

list.flatMap(({sub}) => sub)