Created
December 21, 2016 11:50
-
-
Save rabeesh/59ea9e70adf6cd121164658238deca2d 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
// Recursively concat nested array | |
function concatR(ar, result) { | |
result = result || [] | |
for(var i=0; i < ar.length; i++) { | |
if (Array.isArray(ar[i])) { | |
// calling with inner array | |
concatR(ar[i], result) | |
} else { | |
result.push(ar[i]) | |
} | |
} | |
return result | |
} | |
// For simple test assert | |
function assert(ar, json) { | |
var ok = JSON.stringify(concatR(ar)) == json | |
if (ok) { | |
console.info("Passed: ", json) | |
} else { | |
console.error("Failed: ", json) | |
} | |
} | |
assert([[1,2,[3]],4], "[1,2,3,4]") | |
assert([[1,2,[3,[]]],4], "[1,2,3,4]") | |
assert([1,2,3,4], "[1,2,3,4]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment