Skip to content

Instantly share code, notes, and snippets.

@gwmccull
Last active September 30, 2023 03:18
Show Gist options
  • Save gwmccull/0ee5acb7019266bce29034c4cb9b0c19 to your computer and use it in GitHub Desktop.
Save gwmccull/0ee5acb7019266bce29034c4cb9b0c19 to your computer and use it in GitHub Desktop.
Flatten array of arrays
// 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