Last active
November 23, 2019 13:46
-
-
Save nachodd/5879c68a3a62b317b4ac36f69257cb01 to your computer and use it in GitHub Desktop.
Simple multidimensional array flatten
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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