flat() low version browser compatibility issue, flat() is not function

Google 68 reports an error Array.from(…).flat is not a function
Solution 1: Upgrade the browser directly, and there will be no compatibility issues
Solution 2: Manually write a falt function to the prototype of Array and put it in the referenced file

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;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43485503/article/details/130267795