Skip to content

Instantly share code, notes, and snippets.

@mauriciosoares
Created February 23, 2018 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauriciosoares/2093a4d36c7cce8387aedf328ce2ba7a to your computer and use it in GitHub Desktop.
Save mauriciosoares/2093a4d36c7cce8387aedf328ce2ba7a to your computer and use it in GitHub Desktop.
const array = [[1,2,[3]],4];
function flatten(arr) {
return arr.reduce((newArray, item) => {
return [
...newArray,
...(Array.isArray(item) ? flatten(item) : [item])
]
}, []);
}
flatten(array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment