Skip to content

Instantly share code, notes, and snippets.

@manduks
Created March 7, 2017 20:28
Show Gist options
  • Save manduks/8bf532c800cc310a2bf8ec990264019d to your computer and use it in GitHub Desktop.
Save manduks/8bf532c800cc310a2bf8ec990264019d to your computer and use it in GitHub Desktop.
Flattens an array of integers like [[1,2,[3]],4] -> [1,2,3,4].
/**
* Flattens an array of integers like [[1,2,[3]],4] -> [1,2,3,4].
* @param {array} inputArray The array of integers and/or array of integers.
* @returns {array} Flatten array.
*/
function arrayFlatten(inputArray) {
const arrayOfStrings = inputArray.join(',').split(',');
return arrayOfStrings.map(x => parseInt(x, 10));
}
// test arrayFlatten
console.log(arrayFlatten([1, 5, [6, [7, 9, 8]], [4, 6, 8, [3, 5, 8, 9, [2, 4, 6]]]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment