Created
October 21, 2011 16:38
-
-
Save getify/1304287 to your computer and use it in GitHub Desktop.
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_array(arr) { | |
var i; | |
for (i=0; i<arr.length; ) { | |
if (Object.prototype.toString.call(arr[i]) == "[object Array]") { | |
// prepend `splice()` arguments to `tmp` array, to enable `apply()` call | |
arr.splice.apply(arr,[i,1].concat(arr[i])); | |
continue; | |
} | |
i++; | |
} | |
return arr; | |
} | |
flatten_array([1,2,[3,[4,5],[],6,[[[7]]]]]); // [1,2,3,4,5,6,7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sorry, must have missed something, seems to work. thanks! updating my snippet now accordingly.