Skip to content

Instantly share code, notes, and snippets.

@web2ls
Last active October 31, 2017 15:44
Show Gist options
  • Select an option

  • Save web2ls/daca0b659ecb41ba6b34ff224f2722ad to your computer and use it in GitHub Desktop.

Select an option

Save web2ls/daca0b659ecb41ba6b34ff224f2722ad to your computer and use it in GitHub Desktop.
Turn array into flat structure via Array.prototype.reduce
function flatten(arr) {
if (Array.isArray(arr)) {
return arr.reduce(function(done,curr){
return done.concat(flatten(curr));
}, []);
} else {
return arr;
}
}
console.log(flatten([[1,2,3], [4,5,6],[7,8,9]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment