数组扁平化处理

function flatten(arr) {
    let toString = Object.prototype.toString;
    let res = [];
    let len = arr.length;
    let current = '';

    for (let j = 0; j < len;) {
        current = arr[j++];
        forControl(current);
    }
    function forControl(curr) {
        if (curr) {
            if (toString.call(curr) === '[object Array]') {
                if (curr.length > 0) {
                    cartwheel(curr);
                }
            } else {
                res.push(curr);
            }
        }
    }
    function cartwheel(c) {
        let thatCurrent;
        let cLen = c.length;
        for (let i = 0; i < cLen;) {
            thatCurrent = c[i++];
            forControl(thatCurrent);
        }
    }
    return res;
}

猜你喜欢

转载自www.cnblogs.com/wuxiexy/p/12956174.html