flat()低版本浏览器兼容问题,flat() is not function

谷歌68 报错Array.from(…).flat is not a function
解决方法1:直接升级浏览器,就没有兼容问题了
解决方法2:手动写一个falt函数给Array的原型,放在引用的文件里

if (!Array.prototype.flat) {
    
    
    Array.prototype.flat = function(count) {
    
    
        let c = count || 1;
        let len = this.length;
        let exe = [];
        if (this.length == 0) return this;
        while (c--) {
    
    
            let _arr = [];
            let flag = false;
            if (exe.length == 0) {
    
    
                flag = true;
                for (let i = 0; i < len; i++) {
    
    
                    if (this[i] instanceof Array) {
    
    
                        exe.push(...this[i]);
                    } else {
    
    
                        exe.push(this[i]);
                    }
                }
            } else {
    
    
                for (let i = 0; i < exe.length; i++) {
    
    
                    if (exe[i] instanceof Array) {
    
    
                        flag = true;
                        _arr.push(...exe[i]);
                    } else {
    
    
                        _arr.push(exe[i]);
                    }
                }
                exe = _arr;
            }
            if (!flag && c == Infinity) {
    
    
                break;
            }
        }
        return exe;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43485503/article/details/130267795