Created
January 13, 2019 17:15
-
-
Save BilalAlGhazi/dd9c7810e535f28491efd424168e580b to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Function to flatten an array of arbitrarily nested arrays of integers into a flat array of integers | |
* | |
* @param {Array} inputArray The array to be flattenned. | |
* @param {Array} resultArray The result array. | |
*/ | |
flattenArray = (inputArray, resultArray) => { | |
inputArray.forEach(function(entry) { | |
if(entry.constructor === Array){ | |
flattenArray(entry, resultArray); | |
} else { | |
resultArray.push(entry); | |
} | |
}); | |
} | |
// Example usage | |
const a = [[1,2,[3]],4]; | |
const rslt = []; | |
flattenArray(a, rslt); | |
console.log(rslt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment