Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//sample tree structure
var tree = [
{
id : "A",
list : [
{
id : "A.A",
list : [
{
id : "A.A.A",
list : [
{
id : "A.A.A.A",
list : []
}
]
}
]
},
{
id : "A.B",
list : [
{
id : "A.B.A",
list : []
}
]
}
]
},
{
id : "B",
list : []
}
];
/**
* recursive function to parse array
*/
function parseTree(array, level){
level+=1;
for(var i=0, len = array.length; i < len; i++){
document.body.appendChild(document.createTextNode("Level---"+level+" "+array[i].id));
document.body.appendChild(document.createElement("br"));
if(array[i].list.length > 0){
parseTree(array[i].list,level);
}
}
}
//All the action...
parseTree(tree, 0);
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.