Created
January 21, 2017 00:16
-
-
Save michaelWagner/16e5438004ed1d1b48eaf6405a962d3e to your computer and use it in GitHub Desktop.
Flattens an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]
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
function flatten(array) { | |
let flat_array = []; | |
let len_array = array.length; | |
for (let i = 0; i < len_array; i++) { | |
if (Array.isArray(array[i])) { | |
flat_array = flat_array.concat(flatten(array[i])); | |
} else { | |
flat_array.push(array[i]); | |
} | |
} | |
return flat_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment