function flatten(arr){
var result = [];
if(!arr.length) return [];
for(var i in arr){
if(typeof arr[i] === 'object'){
result = result.concat(flatten(arr[i]));
}else{
result.push(arr[i]);
}
}
return result;
}
var arr = [1,2,3,[4,5,6]];
console.log(arr); // [1, 2, 3, Array(3)]
console.log(flatten(arr)); // [1, 2, 3, 4, 5, 6]
网友评论