方法一
//利用toString()
let arr = [1,2,[3,4],[5,6,[7,8,[9,10]]]];
let res1 = arr.toString().split(",");
let res2 = []
res1.forEach(function(item,index){
res2.push(parseInt(item))
})
console.log(res2)
方法二
//利用递归
let arr = [1,2,[3,4],[5,6,[7,8,[9,10]]]];
let res = [];
function toArr(_arr){
_arr.forEach(function(item,index){
if(Object.prototype.toString.call(item).indexOf("Array")>-1){
fu(item)
}else{
res.push(item)
}
})
}
fu(arr)
console.log(res)
网友评论