Created
August 24, 2015 17:45
-
-
Save johntran/b4c74294ff8766f25fb1 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
// Given an array with nested arrays, return a non-nested array. | |
function flatten(collection) { | |
var result = []; | |
var getValues = function(collection, result) { | |
for (var key in collection) { | |
if (!Array.isArray(collection[key])) { | |
result.push(collection[key]); | |
} else { | |
getValues(collection[key], result); | |
} | |
} | |
}; | |
getValues(collection, result); | |
return result; | |
} | |
var testArray3 = [1, 2, [3, 4], | |
[5, [6, 7]] | |
]; | |
flatten(testArray3); | |
var testArray = [1, 2, 3, [4]]; | |
var testArray2 = [1, 2, 3, [4, 5]]; | |
module.exports=flatten; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment