可用方法
<script type="text/javascript">
var array=[1,[3,[4],[5,[6,7]],8],[9,[10]]];
var newarray=[];
function flat(array,newarray){
for (var i = 0; i < array.length; i++) {
//类型为number, 则放入到新数组中
if(typeof array[i]=="number"){
newarray.push(array[i]);
}else{
//否则, 继续分解, 判断, 放入....
flat(array[i],newarray);
}
}
console.log(newarray);
}
flat(array,newarray);
</script>
也是一种方法但是条件受限, 仅供参考
<script type="text/javascript">
var array=[1,[3,[4],[5,[6,7]],8],[9,[10]]];
var str=array.toString();
//"string" "1,3,4,5,6,7,8,9,10"
var newarray=str.split(",");
//newarray: ["1", "3", "4", "5", "6", "7", "8", "9", "10"]
for (var i = 0; i < newarray.length; i++) {
newarray[i]=parseInt(newarray[i]);
// "string"->"number"
}
console.log(newarray);
</script>