Created
October 23, 2017 10:16
-
-
Save isaachinman/068c01daa8682fb441121faf4dd62476 to your computer and use it in GitHub Desktop.
A utility function to flatten a deeply nested array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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