Created
October 16, 2017 14:35
-
-
Save qborreda/8f4883e90d48f52bba77feccd21d2ea0 to your computer and use it in GitHub Desktop.
Flatten an array of numbers
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 flattenArray(arr) { | |
let found = []; | |
arr.map((elem, index) => { | |
if (Array.isArray(elem)) { | |
found = found.concat(flattenArray(elem)); | |
} else { | |
found.push(elem); | |
} | |
}); | |
return found; | |
} | |
let testArray = [[1, 2, [3]], 4, [5, 6]]; | |
let output = flattenArray(testArray); | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment