Mark As Completed Discussion

Good evening! Here's our prompt for today.

In Javascript there is a method for flattening an array that can be invoked by calling the function flat(array, depth). This function works in such a way that it reduces the nesting of a given array, by creating a new array with all sub-array elements concatenated into it recursively up to the given depth level.

This means that if there are multiple nested arrays inside a given array, it would return an array containing only the elements of the main array itself, and the elements from the nested arrays that are placed inside the given depth level.

Here is an example of what this method does:

JAVASCRIPT
1array = [1, [2], [3, [4]]];
2flat(array, 1)
3// [1, 2, 3, [4]] flattens on depth level 1
4flat(arr, 2)
5// [1, 2, 3, 4] flattens on depth level 2

Can you implement your own version of this method, that will receive two parameters: an array, and an integer for the depth level, and would have the following structure:

JAVASCRIPT
1function flat(array, depthLevel = 1) {
2    let result = [];
3    // your implementation here
4    return result;
5}

Question

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

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

Here's our guided, illustrated walk-through.

How do I use this guide?

Solution

We can easily implement this method as a recursive function that will iterate over each element of the array, and check if that element is an array. As we said before, an element of the input array can be either a normal object, or an array that is nested and needs to be flattened.

If the element is a array, we will call the method recursively for that array, reducing the depthLevel by one. This way, we would start from the top level, and work our way into the lower depth levels until we reach the given depth level, while filling the resulting array with the necessary elements.

Otherwise, if the item itself is not an array, we would consider it being an element of the given array, will directly push it into the resulting array, and continue looping the following elements of the given array, to check if any nested ones are left.

This being said, our final solution would look like this:

JAVASCRIPT
1function flat(array, depthLevel = 1) {
2    let result = [];
3    array.forEach((item) => {
4        // if the item of the main array is an array itself, call the method recursively 
5        // to check the elements inside
6        if (Array.isArray(item) && depthLevel > 0) {
7            result.push(...flat(item, depthLevel - 1));
8        } else result.push(item); // else, push the object into the result
9    });
10    return result;
11}

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

That's all we've got! Let's move on to the next tutorial.

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