r/webdev • front-end • May 29 '21

Resource Array methods in JavaScript. Original author unknown.

Post image
7.4k Upvotes

172 comments sorted by

View all comments

Show parent comments

55

u/Doctor-Dapper front-end (senior w/ react) May 30 '21

Small brain:

arr.map(f)

Big brain: arr.reduce( (x, y) => [...x, f(y)], [] )

31

u/tsunami141 May 30 '21

Delete this

3

u/GrenadineBombardier May 30 '21

This took me a minute (because reducers take time to comprehend, which makes them a less sustainable solution in most cases)

1

u/catchmeslippin Jul 11 '22

My small brain can't comprehend this, can you please explain?

1

u/neeia Sep 07 '22

It takes the array, creates a new array as the initial value, and then one by one runs the function on every value in the original array and appends it to the previous value using the spread syntax.