Skip to content

Instantly share code, notes, and snippets.

@fcpauldiaz
Created August 15, 2019 16:53
Show Gist options
  • Save fcpauldiaz/ed474f54de006f5b9b69020b6cc64b5d to your computer and use it in GitHub Desktop.
Save fcpauldiaz/ed474f54de006f5b9b69020b6cc64b5d to your computer and use it in GitHub Desktop.
function flatten (array) {
// check that input is an array
if (array.constructor === Array) {
return array.reduce((accum, currentValue) => {
if (array.constructor === Array) {
// flatten the array using es6 reducer
return accum.concat(currentValue.reduce(flatten, []))
} else {
// append/concat
return accum.concat(currentValue)
}
}
, []
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment