Skip to content

Instantly share code, notes, and snippets.

@nelsonomuto
Last active April 28, 2018 03:49
Show Gist options
  • Save nelsonomuto/ddf26ae5582ceaa5065478867d60491b to your computer and use it in GitHub Desktop.
Save nelsonomuto/ddf26ae5582ceaa5065478867d60491b to your computer and use it in GitHub Desktop.
(function flatten(arr, currentIndex = 0, flattened = []) {
let currItem;
if (currentIndex >= arr.length) {
return flattened;
}
currItem = arr[currentIndex++];
if (!Array.isArray(currItem)) {
flattened.push(currItem);
} else {
flattened = flattened.concat(flatten(currItem));
}
return flatten(arr, currentIndex, flattened);
})([[43,[5,6],7], 10]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment