Skip to content

Instantly share code, notes, and snippets.

@jzombie
Last active January 3, 2017 21:18
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 jzombie/be96c5f2a17860ff7b141ed50fe06fd2 to your computer and use it in GitHub Desktop.
Save jzombie/be96c5f2a17860ff7b141ed50fe06fd2 to your computer and use it in GitHub Desktop.
function flatten(arr) {
// The reduce() method reduces the array to a single value
// @see http://www.w3schools.com/jsref/jsref_reduce.asp
return arr.reduce(function (flat, toFlatten) {
// The concat() method is used to merge two or more arrays.
// This method does not change the existing arrays, but instead returns a new array.
// @see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
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