Last active
March 3, 2017 12:59
-
-
Save mrtcode/9d18c192bd3106092cd59dc3f4f9ebd7 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
var a = [[1,2,[3]],4]; | |
function flat(a) { | |
function _flat(a, depth) { | |
var b = []; | |
if(depth>100) throw 'too deep'; | |
for(var i=0;i<a.length;i++) { | |
var c = a[i]; | |
if(Array.isArray(c)) { | |
b = b.concat(_flat(c, depth+1)) | |
} else { | |
b.push(c); | |
} | |
} | |
return b; | |
} | |
try { | |
return _flat(a, 0); | |
} catch(e) { | |
return e; | |
} | |
} | |
var ret = flat(a); | |
if(Array.isArray(ret)) { | |
console.log(ret); | |
} else { | |
console.log('error:', ret); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment