javascript - jquery Looping (.each) -


i have object looks following:

node{      name: "root",      level: 0,      children: array[14],      parent: null,      id: 0 } 

and inside node.children...

node.children[     {      name: "child1",      level: 1,      children: array[1],      parent: root,      id: 1     },     {      name: "child2",      level: 1,      children: array[1],      parent: root,      id: 2     },     {      name: "child3",      level: 1,      children: array[2],      parent: root,      id: 3     }, ] 

and inside node.children[1].children ...

node.children[1].children[         {          name: "child1-1",          level: 2,          children: array[0],          parent: child1,          id: 4         } ] 

what need loop through node object , try , match every "id" value given. example...

$.each(node, function(i, nodes){     $.each(nodes, function (i2, nodes2){         if (nodes2.id == 5){             //do         }     }) }) 

you'll want function can call recursively:

function checknode(node, action, id) {   if (node.id === id)     action(node);    var kids = node.children || [];    $.each( kids,      function(i,n) {       checknode(n, action, id);     }   ); } 

called as:

checknode(    node,    function(n) { alert(n.name); },    5 ); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -