Skip to content

Instantly share code, notes, and snippets.

@praveenpuglia
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveenpuglia/ef13ad6bbad14204a73c to your computer and use it in GitHub Desktop.
Save praveenpuglia/ef13ad6bbad14204a73c to your computer and use it in GitHub Desktop.
//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