Skip to content

Instantly share code, notes, and snippets.

@rmfranciacastillo
Created January 16, 2019 06:38
Show Gist options
  • Save rmfranciacastillo/6a9abdad606c943236f8886aef8b5a02 to your computer and use it in GitHub Desktop.
Save rmfranciacastillo/6a9abdad606c943236f8886aef8b5a02 to your computer and use it in GitHub Desktop.
Flattens an Array using JS
let arr = [[1,2,[3]], [[[4]]]];
let flatten = arr.reduce((acc, value) => acc.concat(value), []);
while(flatten.some((item) => Array.isArray(item))){
console.log(flatten);
flatten = flatten.reduce((acc, value) => acc.concat(value), []);
}
console.log(flatten)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment