Skip to content

Instantly share code, notes, and snippets.

@manimal1
Last active November 26, 2019 10:33
Show Gist options
  • Save manimal1/b6deb95f438076702085c3b759e3230b to your computer and use it in GitHub Desktop.
Save manimal1/b6deb95f438076702085c3b759e3230b to your computer and use it in GitHub Desktop.
JavaScript utility function to flatten an array of arrays
export const flattenArray = (arr, depth = arr.length) => {
return depth > 0
? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flattenArray(val, depth - 1) : val), [])
: arr.slice();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment