Skip to content

Instantly share code, notes, and snippets.

@isaachinman
Created October 23, 2017 10:16
Show Gist options
  • Save isaachinman/068c01daa8682fb441121faf4dd62476 to your computer and use it in GitHub Desktop.
Save isaachinman/068c01daa8682fb441121faf4dd62476 to your computer and use it in GitHub Desktop.
A utility function to flatten a deeply nested array
export default function flattenDeeplyNestedArray(array) {
return array.reduce((flattenedArray, value) => {
if (Array.isArray(value)) {
// If our value is an array itself, flatten again before concating
return flattenedArray.concat(flattenDeeplyNestedArray(value))
} else {
// Otherwise simply concat it to the reducing array
return flattenedArray.concat(value)
}
}, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment