Created
February 12, 2019 17:31
-
-
Save hai5nguy/79c1e0e323a9a9695696f0c8923b6918 to your computer and use it in GitHub Desktop.
hai nguyen - citrusbyte coding question
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 sampleArray = [[1,2,3],[[4],[5,6], [7,[8],9]], [10, 11], [12, [13, 14]]] | |
function flatten(array) { | |
var result = []; | |
recurseFlatten(array, result);; | |
return result; | |
} | |
function recurseFlatten(array, result) { | |
for (var i = 0; i < array.length; i++) { | |
var item = array[i]; | |
if (Array.isArray(item)) { | |
recurseFlatten(item, result); | |
} else { | |
result.push(item); | |
} | |
} | |
} | |
console.log(flatten(sampleArray)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment