Skip to content

Instantly share code, notes, and snippets.

@Natumsol
Created April 1, 2015 01:48
Show Gist options
  • Save Natumsol/689a95caa670b9919652 to your computer and use it in GitHub Desktop.
Save Natumsol/689a95caa670b9919652 to your computer and use it in GitHub Desktop.
flatten a JavaScript array
function flatten(arr) {
var result = [];
for(var i = 0, length = arr.length; i < length; i ++ ){
var value = arr[i];
if(!Array.isArray(value)){
result.push(value);
} else {
result = result.concat(flatten(value));//递归调用
}
}
return result;
}
console.log(flatten([1,[2,3,[4,5,6,[2,[23232,[323,232,[232]]]]]]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment