Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.