[JavaScript] A simple recursive solution
Overall Idea\n\nThis problem lends very well for a recursive solution! We iterate through the array, checking for nested arrays within.\n\n1) If the element is 
# Overall Idea\n\nThis problem lends very well for a recursive solution! We iterate through the array, checking for nested arrays within.\n\n1) If the element is another array, we call it recursively down one level. We can use the spread operator (`...`) to do the flattening for us when appending to our flattened array.\n2) Otherwise, we can simply push the element.\n\nRemember to stop the recursion when `n = 0`!\n\n# Code\n```js\n// A normal, recursive approach.\nconst flat = function (arr, n) {\n    if (n === 0)\n        return arr;\n    let res = [];\n    for (let i in arr) {\n        if (Array.isArray(arr[i]))\n            res.push(...flat(arr[i], n - 1));\n        else\n            res.push(arr[i]);\n    }\n    return res;\n}\n```\n\n## Considerations & Optimizations\n\nWe could also get away with not allocating an array at every recursive call! Instead, we can just define an inner recursive function that appends to an outer array for us.\n\nThe rest of the algorithm is more or less the same:\n\n```js\n// An optimization - saves unnecessary\n// array allocations at every call!\nconst flat = function (arr, n) {\n    let res = [];\n    const recur = (arr, n) => {\n        for (let i in arr) {\n            if (n > 0 && Array.isArray(arr[i]))\n                recur(arr[i], n - 1);\n            else\n                res.push(arr[i]);\n        }\n    }\n    recur(arr, n);\n    return res;\n}\n```\n\n# Complexity\n\n**Time complexity:** $$O(n)$$ - Unless `n = 0`, we wind up checking every element in the input array.\n**Space complexity:** $$O(n)$$ - Besides the space required for the output array, we allocate a function call on the stack for every recursive call.
['Recursion', 'JavaScript']