100% Simple and Easy Solution | Faster Recursive Solution
Complexity
Time complexity: O(N * D)

Space complexity: O(N)
Code
# Complexity
- Time complexity: O(N * D)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
/**
 * @param {Array} arr
 * @param {number} depth
 * @return {Array}
 */

var flat = function (arr, n, res = []) {
   
    for(let i = 0; i < arr.length; i++) {
        if(Array.isArray(arr[i]) && n)
            flat(arr[i], n - 1, res);
        else
            res.push(arr[i]);
    }

    return res;
    
};
```
['JavaScript']