Created
October 24, 2011 06:38
-
-
Save handlename/1308489 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var array = [ | |
1, | |
2, | |
[10, 11, 12], | |
[20, 21, [30, 31]], | |
3 | |
]; | |
console.log('before'); | |
console.log(array); | |
flatten(array, function(res) { | |
console.log('after'); | |
console.log(res); | |
}); | |
function flatten(array, callback) { | |
var done = []; | |
var doneNum = 0; | |
function check(res) { | |
done = done.concat(res); | |
++doneNum; | |
if (array.length <= doneNum) { | |
callback(done); | |
} | |
} | |
for (var i = 0; i < array.length; ++i) { | |
if (array[i] instanceof Array) { | |
flatten(array[i], function(res) { | |
check(res); | |
}); | |
} | |
else { | |
check([array[i]]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment