DEV Community

Takuya Kikuchi
Takuya Kikuchi

Posted on

reduce() - Flatten an array of arrays

Time to play with reduce() 🥳
I will try some functions with reduce() to deepen my understanding about reduce()

Array.prototype.reduce() - JavaScript | MDN

Flatten an array of arrays

To flat single level array
!Deep level flatten is not applicable with this function
=> The alternative is Array.prototype.flat() - JavaScript | MDN (Thanks @jcubic for your comment 😉)

Image 2021-08-04 at 7.20.15 PM

Play it by yourself

Discussion (2)

jcubic profile image
Jakub T. Jankiewicz • Edited

There is Array.prorotype.flat() that do the same and more because it can do nested arrays of any level.

takuyakikuchi profile image
Takuya Kikuchi Author

Excellent point!
I have added the information 👍

And I think if you want to flatten an array, using flat() is more intuitive and readable than doing it with reduce().

Anyway, thanks for your comment!