Skip to content

Instantly share code, notes, and snippets.

@mhfowler
Created October 9, 2016 23:03
Show Gist options
  • Save mhfowler/24d1ccb3cb4c034bbf735a3a9b094d7b to your computer and use it in GitHub Desktop.
Save mhfowler/24d1ccb3cb4c034bbf735a3a9b094d7b to your computer and use it in GitHub Desktop.
flattenArr(arr):
# base case
## how to check if a variable is an array http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript
if (arr.constructor != Array) {
return [arr];
}
# recursion
else {
foundElements = []
for (var i = 0; i < arr.size; i++) {
elts = flattenArr(arr[i])
foundElements = foundElements.concat(elts)
}
return foundElements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment