Skip to content

Instantly share code, notes, and snippets.

@johntran
Created August 24, 2015 17:45
Show Gist options
  • Save johntran/b4c74294ff8766f25fb1 to your computer and use it in GitHub Desktop.
Save johntran/b4c74294ff8766f25fb1 to your computer and use it in GitHub Desktop.
// 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