Created
September 23, 2017 10:58
-
-
Save batogov/67462330bafe6f4999aaf24523e269db to your computer and use it in GitHub Desktop.
Make Array Flat Again!
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
var result = []; | |
function flatten(val) { | |
if (Array.isArray(val)) { | |
for (var i = 0; i < val.length; i++) { | |
flatten(val[i]); | |
} | |
} else { | |
result.push(val); | |
} | |
} | |
var arr = [1, 2, [3, 4, [5, 6, 7]]]; | |
flatten(arr); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment