Created
April 1, 2015 01:48
-
-
Save Natumsol/689a95caa670b9919652 to your computer and use it in GitHub Desktop.
flatten a JavaScript array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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