Skip to content

Instantly share code, notes, and snippets.

@adrianbona
Last active April 24, 2019 22:04
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 adrianbona/3302bb0161cc4eaa6bbf695ead298a13 to your computer and use it in GitHub Desktop.
Save adrianbona/3302bb0161cc4eaa6bbf695ead298a13 to your computer and use it in GitHub Desktop.
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers
/**
* Flattens an array.
*
* Flattens an array of arbitrarily nested arrays of integers into a flat array of integers.
*
* @param {array} myArray Array to be flattened.
*
* @return {array} Flattened version of input array.
*/
function flattenMyArray(myArray) {
let result = [];
for (let i = 0; i < myArray.length; i++) {
if (Array.isArray(myArray[i])) {
result = result.concat(flattenMyArray(myArray[i]));
} else {
result.push(myArray[i]);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment