自制版flat方法

78 阅读1分钟
        Array.prototype.brandNewFlat = function (n = 1) {
            let result = this
            while (result.some(item => Array.isArray(item)) && n) {
                result = result.reduce((arr, item) => {
                    if (Array.isArray(item)) return [...arr, ...item]
                    return [...arr, item]
                }, [])
                n--;
            }
            return result
        }