Skip to content

Instantly share code, notes, and snippets.

@rabeesh
Created December 21, 2016 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rabeesh/59ea9e70adf6cd121164658238deca2d to your computer and use it in GitHub Desktop.
Save rabeesh/59ea9e70adf6cd121164658238deca2d to your computer and use it in GitHub Desktop.
// 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