Skip to content

Instantly share code, notes, and snippets.

@veeracs
Created December 15, 2018 13:46
Show Gist options
  • 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