Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Created December 19, 2017 07:40
Show Gist options
  • Save black-black-cat/ed5799c80a745b85d9d9b4acfeaec52f to your computer and use it in GitHub Desktop.
Save black-black-cat/ed5799c80a745b85d9d9b4acfeaec52f to your computer and use it in GitHub Desktop.
flatten array
function flatten(arr, result) {
result = result || []
arr.forEach(function(item) {
if (Array.isArray(item)) {
flatten(item, result)
} else {
result.push(item)
}
})
return result
}
// test
console.log(flatten([
[3],
[[5], [[[9]]]]
]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment