Skip to content

Instantly share code, notes, and snippets.

@veeracs
Created December 15, 2018 13:46
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 veeracs/a11f0cb6a602823f37ae8d1cb844b96a to your computer and use it in GitHub Desktop.
Save veeracs/a11f0cb6a602823f37ae8d1cb844b96a to your computer and use it in GitHub Desktop.
Flatten Arrays using Reducer
const myarr = [1,2,[3]];
function flatten(arr) {
if (Array.isArray(arr)) {
return arr.reduce((prev, next) => {
return prev.concat(flatten(next));
}, []);
} else {
return arr;
}
}
console.log(flatten(myarr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment