SOURCE

function flat(list) {
    if (!Array.isArray(list)) return [];
    const stack = [[0, list]];
    const result = [];
    while (stack.length) {
        const [idx, arr] = stack.pop();
        if (arr.length <= idx) continue;
        for (let i = idx; i < arr.length; i++) {
            if (Array.isArray(arr[i])) {
                stack.push([i + 1, arr], [0, arr[i]]);
                break;
            }
            result.push(arr[i]);
        }
    }
    return result;
}

console.log(flat([1, 2, [3], [4], [5, 6, 7, [8, 9, [10, 11, 12], [13], [14, 15, [16, 17]]]]]))
console 命令行工具 X clear

                    
>
console