Last active
September 30, 2023 03:18
-
-
Save gwmccull/0ee5acb7019266bce29034c4cb9b0c19 to your computer and use it in GitHub Desktop.
Flatten array of arrays
This file contains 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
// returns a flattened array | |
function flattenArray(arr = []) { | |
return arr.reduce((prev, value) => { | |
if (Array.isArray(value)) { | |
// make a recursive call to flatten the inner array | |
return [...prev, ...flattenArray(value)]; | |
} | |
return [...prev, value]; | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment