Skip to content

Instantly share code, notes, and snippets.

@felipernb
Created August 16, 2011 08:38
Show Gist options
  • Save felipernb/1148670 to your computer and use it in GitHub Desktop.
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
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