Skip to content

Instantly share code, notes, and snippets.

@nicolas-amabile
Last active April 3, 2019 16:48
Show Gist options
  • Save nicolas-amabile/cc0e393950159319271121ba9b89e630 to your computer and use it in GitHub Desktop.
Save nicolas-amabile/cc0e393950159319271121ba9b89e630 to your computer and use it in GitHub Desktop.
Flatten an array of nested arrays
/*
* This function returns a flat array given an array of arbitrarily nested arrays
* e.g. [[1,2,[3]],4] -> [1,2,3,4].
*/
const flattenArray = arr => {
const result = []
arr.map(item => {
if (Array.isArray(item)) {
result.push(...flattenArray(item))
} else {
result.push(item)
}
})
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment