Created
May 25, 2016 03:18
-
-
Save ile/3652fe63b769c3d5838d76d533a32a17 to your computer and use it in GitHub Desktop.
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
function flatten(arr) { | |
var arr2 = []; | |
function getValue(val) { | |
if (Array.isArray(val)) { | |
val.forEach(getValue) | |
} | |
else { | |
arr2.push(val); | |
} | |
} | |
arr.forEach(getValue); | |
return arr2; | |
} | |
flatten([[1,2,[3]],4]); // [1, 2, 3, 4] | |
flatten([[1,2,[3, 4, [5, 6]]], 7, [8]]); // [1, 2, 3, 4, 5, 6, 7, 8] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment