Skip to content

Instantly share code, notes, and snippets.

@guzmonne
Created November 15, 2017 19:38
Show Gist options
  • Save guzmonne/aa659842607a800accca17b979c0c6f2 to your computer and use it in GitHub Desktop.
Save guzmonne/aa659842607a800accca17b979c0c6f2 to your computer and use it in GitHub Desktop.
flatten.js
/**
* Flattens an array of arbitrarily nested arrays into a flat array.
* E.g. [[1,2,[3]],4] -> [1,2,3,4].
* @param {array} array Array to be flatten.
*/
function flatten(array) {
array = array.reduce((acc, x) => acc.concat(x), []);
return array.every(x => Array.isArray(x) === false) === true
? array
: flatten(array)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment