Created
March 7, 2017 20:28
-
-
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].
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
/** | |
* 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