数组的扁平化,是将一个嵌套多层的数组 array (嵌套可以是任何层数)转换为只有一层的数组
function flatten1(arr) {return arr.join(',').split(',').map(function(item) {return Number(item);})}function flatten2(arr) {var newArr = [];arr.map(item => {if(Array.isArray(item)){newArr.push(...flatten2(item))} else {newArr.push(item)}})return newArr}function flatten3(arr) {let stack = [...arr].reverse()let newArr = []while(stack.length){let o = stack.pop()if(Array.isArray(o)){stack.push(...o.reverse())} else {newArr.push(o)}}return newArr}function flatten4(arr) {while(arr.some(item=>Array.isArray(item))) {arr = [].concat(...arr);}return arr;}flatten1([3,[4,8,[9,1],3],[6,8],[2,10],5,7])flatten2([3,[4,8,[9,1],3],[6,8],[2,10],5,7])flatten3([3,[4,8,[9,1],3],[6,8],[2,10],5,7])flatten4([3,[4,8,[9,1],3],[6,8],[2,10],5,7])
