function flatten(array) {
return array.reduce((flat, current)=>{
if(current instanceof Array){
return flat.concat(flatten(current));
} else{
return [...flat, current];
}
},[])
}
let a = flatten([1,2,3,[4,5,[6,7],8],9]);
console.log(a);
原生:
var newArray = arr.flat([depth]);
网友评论