Skip to content

Instantly share code, notes, and snippets.

@marcoslhc
Last active March 13, 2017 03:09
Show Gist options
  • Select an option

  • Save marcoslhc/a01edd52d51d9fb501b3d940d9aca1a8 to your computer and use it in GitHub Desktop.

Select an option

Save marcoslhc/a01edd52d51d9fb501b3d940d9aca1a8 to your computer and use it in GitHub Desktop.
flattenArray.js
function flattenArray(array) {
let temp = [];
function flatten(arr) {
if (Array.isArray(arr)) {
const [head, tail] = [arr[0], arr.slice(1)]
flatten(head);
if (tail.length > 0) flatten(tail);
} else {
temp.push(arr);
}
}
flatten(array);
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment