数组扁平化
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; }
声明:严禁抄袭,欢迎转载!不过请带上博文链接!
或者你有更好的实现方式、或者你觉得有可以优化的地方,不妨评论讨论下!觉得得到帮助的可以点个推荐,让更多人也可以得到帮助。