Skip to content

Instantly share code, notes, and snippets.

@mingca
Created October 20, 2017 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingca/52fc2da1902ee54177b9944b26b4a29d to your computer and use it in GitHub Desktop.
Save mingca/52fc2da1902ee54177b9944b26b4a29d to your computer and use it in GitHub Desktop.
/*
Flatten Array - exactly same with Ruby's Array#flatten
Example:
flatten([[1, 2, 3], [4, 5]]); // [1, 2, 3, 4, 5]
*/
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment