// 定于无限常量
const INFINITY = 1/0;

// 定义用于判断是否可继续摊平的方法
function isFlattenable(value){
    return Array.isArray(value)
}

function flatMapDeep(array){
    return baseFlatten(array, INFINITY)
}

function baseFlatten(array, deep, result){
    result || (result = [])
    if(array == null) return result
    for(const value of array){
        if(deep > 0 && isFlattenable(value)){
            if(deep > 1){
                baseFlatten(value, deep - 1, result)
            }else{
                result.push(...value)
            }
        }else{
            result[result.length] = value
        }
    }
    return result 
}

console.log(flatMapDeep([[[1,2,3],[1,2,3]],[5,6]]))