Skip to content

Instantly share code, notes, and snippets.

@jourdanrodrigues
Last active May 9, 2019 18:26
Show Gist options
  • Save jourdanrodrigues/7404e8fc6f48ef444364c061d71969a0 to your computer and use it in GitHub Desktop.
Save jourdanrodrigues/7404e8fc6f48ef444364c061d71969a0 to your computer and use it in GitHub Desktop.
Function to flatten an array object.
/**
* Function to flatten an array object
* Example: [[1, 2, [3]], 4] -> [1, 2, 3, 4]
* @param: {Array} array: Array object to be flattened
*/
function flatten (array) {
return array.reduce((flat, next) => flat.concat(next instanceof Array ? flatten(next) : next), []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment