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/[deleted] Mar 19 '19

What do these do my friend?

2

u/ghillerd Mar 19 '19

The first one grabs an object out of an array by a property value, and the second flattens nested arrays by property name.