Skip to content

Instantly share code, notes, and snippets.

@nachodd
Last active November 23, 2019 13:46
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 nachodd/5879c68a3a62b317b4ac36f69257cb01 to your computer and use it in GitHub Desktop.
Save nachodd/5879c68a3a62b317b4ac36f69257cb01 to your computer and use it in GitHub Desktop.
Simple multidimensional array flatten
/**
* Flatten multidimensional Arrays
* @param {Array} arrayToFlat multidimensional array to be flatten.
* @returns {Array} array flattened
*/
function flatten(arrayToFlat = []) {
const result = []
arrayToFlat.forEach(element => {
if (Array.isArray(element)) {
result.push(...flatten(element))
} else {
result.push(element)
}
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment