ES6方法flat()
let arr = let arr = [1,2,['a','b',['c','d',[2,[2,3,4]]]]];
arr.flat(Infinity);//[1, 2, "a", "b", "c", "d", 2, 2, 3, 4]
遍历
let arr = [1,2,['a','b',['c','d',[2,[2,3,4]]]]];
let newArr = [];
function flatten(arr){
arr.forEach(val=>{
if(Array.isArray(val)){
flatten(val);
}else{
newArr.push(val);
}
})
return newArr
}
网友评论