> For the complete documentation index, see [llms.txt](https://hannahpun.gitbook.io/leetcode-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hannahpun.gitbook.io/leetcode-note/recursion-di-hui/2625.-flatten-deeply-nested-array.md).

# #2625. Flatten Deeply Nested Array

Recursion 經典題, flat the array. 也是實現 arr.flat(n)

[Quetion](https://leetcode.com/problems/flatten-deeply-nested-array/?envType=study-plan-v2\&envId=30-days-of-javascript)

```javascript
/**
 * @param {Array} arr
 * @param {number} depth
 * @return {Array}
 */
 
var flat = function (arr, n) {
    if(n==0) return arr
    let result = [];

    function flatArr(arr, n){
        for(let val of arr){
            if(n>0 && typeof val === "object"){
                flatArr(val, n-1);
            } else {
                result.push(val)
            }
        }
    }
    flatArr(arr, n);
    return result
   
};
```
