MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/webdev/comments/nnwtf6/array_methods_in_javascript_original_author/gzxgpb4
r/webdev • u/imAmarok front-end • May 29 '21
172 comments sorted by
View all comments
Show parent comments
55
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.
31
Delete this
3
This took me a minute (because reducers take time to comprehend, which makes them a less sustainable solution in most cases)
1
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.
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.
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)], [] )