🔥  Fastest & Easiest Solution | Time O(n) > 99% & Extra Space O(n) > 90% ✅
Solution from Pro JS Developer\n\nPlease take a look at the code below.\n\n# Approach\n\nNo rocket sciense, no magic, nothing excess.\nresult - returning array,
# Solution from Pro JS Developer\n\nPlease take a look at the code below.\n\n# Approach\n\nNo rocket sciense, no magic, nothing excess.\n**result** - returning array, accesed from closure.\n**toFlat** - recursion function with same arguments. On each flattening level just passed **n - 1**.\n\n# Complexity\n- Time complexity: O(n) - Runtime: **86 ms** - beats > 99%\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n) - Memory: **61.1 MB** - beats > 90%\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n![rail-sakhaviev-flattern.png](https://assets.leetcode.com/users/images/a57ea553-0299-4be6-b4b4-a87ab388ebdb_1685823541.569171.png)\n\n\n# Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (source, level) {\n    const result = [];\n    const toFlat = (arr, n) => {\n        for (let i = 0; i < arr.length; i++) {\n            if (Array.isArray(arr[i]) && n > 0) {\n                toFlat(arr[i], n - 1);\n            } else {\n                result.push(arr[i]);\n            }\n        }\n    };\n    toFlat(source, level);\n    return result;\n};\n```
['JavaScript']