Skip to content

Instantly share code, notes, and snippets.

@jfensign
Created March 5, 2014 01:16
Show Gist options
  • Save jfensign/9359315 to your computer and use it in GitHub Desktop.
Save jfensign/9359315 to your computer and use it in GitHub Desktop.
Unique paths in tree.
var tree = {
name: "Tree",
children: [
{
name: "1",
children:[
{
name: "1.1"
},
{
name: "1.2",
children: [
{
name:"1.2.1"
},
{
name:"1.2.2"
}
]
}
]
},
{
name: "3",
children: [
{
name:"3.1"
},
{
name:"3.2"
}
]
}
]
},
recurse_tree = function recurse (root, parent) {
var path_prefix = (parent ? (parent+" | ") : "") + root.name;
return root.children
? [].concat.apply ([], root.children.map (function (v) {return recurse (v, path_prefix); }))
: path_prefix;
},
example = function () { console.log (recurse_tree (tree)); } ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment