While loop + splice + typeof
Code


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

    while(n > 0){
        while(index < arr.length){
            if(String(typeof arr[index]) === 'object'){
                count = arr[index].length;
                arr.splice(index, 1, ...arr[index]);
                index += count;
                count = 0;
            }
            else{
                index++;
            }
        }
        index = 0;
        n--;
    }    
    return arr;
};
```
['JavaScript']