Created
August 16, 2011 08:38
-
-
Save felipernb/1148670 to your computer and use it in GitHub Desktop.
Merges an array recursively and join it as a string with the specified glue character
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 stringify = function(arr, glue) { | |
if (glue == undefined) glue = ','; | |
var seq = []; | |
var merge_recursive = function(a) { | |
// Using BFS algorithm as if it was a tree | |
for (var i = 0; i < a.length; i++) { | |
if (Array.isArray(a[i])) { | |
merge_recursive(a[i]); | |
} else { | |
seq.push(a[i]); | |
} | |
} | |
return seq; | |
} | |
return merge_recursive(arr).join(glue); | |
} | |
console.info(stringify(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment