Last active
April 3, 2019 16:48
-
-
Save nicolas-amabile/cc0e393950159319271121ba9b89e630 to your computer and use it in GitHub Desktop.
Flatten an array of nested arrays
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
/* | |
* 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