Mark As Completed Discussion

One Pager Cheat Sheet

  • This tutorial explains how to use the flat() function in Javascript to flatten nested arrays to a specific depthLevel and provides an example of how to implement your own version of this method.
  • We can recursively iterate through an array and flat it to the given depthLevel, while pushing any non-array element directly into the result array.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Alright, well done! Try another walk-through.

If you had any problems with this tutorial, check out the main forum thread here.