angularjs - JavaScript recursion find method -
i've following json:
"params": [ { "name": "a", "value": "tes", "isattr": false, "children": [ { "name": "b", "value": "b", "isattr": false, "uid": "0.529892839025706", "parent_uid": "0.8096382778603584", "children": [], "expanded": true, "level": 2 } ], "uid": "0.8096382778603584", "expanded": true, "level": 1 }, { "name": "c", "value": "c", "isattr": false, "children": [ { "name": "d", "value": "d", "isattr": false, "children": [ { "name": "e", "value": "e", "isattr": false, "children": [ { "name": "f", "value": "f", "isattr": false, "uid": "0.5153285339474678", "parent_uid": "0.646890748757869", "children": [], "expanded": false, "level": 4 } ], "uid": "0.646890748757869", "parent_uid": "0.2041493458673358", "expanded": false, "level": 3 } ], "uid": "0.2041493458673358", "parent_uid": "0.9416675802785903", "expanded": true, "level": 2 }, { "name": "g", "value": "g", "isattr": false, "children": [ { "name": "h", "value": "h", "isattr": false, "uid": "0.3920950344763696", "parent_uid": "0.6195112708956003", "children": [], "expanded": false, "level": 3 } ], "uid": "0.6195112708956003", "parent_uid": "0.9416675802785903", "expanded": true, "level": 2 }, { "name": "i", "value": "i", "isattr": false, "children": [ { "name": "j", "value": "j", "isattr": false, "children": [ { "name": "k", "value": "k", "isattr": false, "children": [ { "name": "l", "value": "l", "isattr": false, "uid": "0.009914681548252702", "parent_uid": "0.68887914926745", "children": [], "expanded": false, "level": 5 } ], "uid": "0.68887914926745", "parent_uid": "0.8186752030160278", "expanded": false, "level": 4 } ], "uid": "0.8186752030160278", "parent_uid": "0.8304482696112245", "expanded": false, "level": 3 }, { "name": "m", "value": "m", "isattr": false, "children": [ { "name": "n", "value": "n", "isattr": false, "uid": "0.25650549842976034", "parent_uid": "0.14876524219289422", "children": [], "expanded": false, "level": 4 } ], "uid": "0.14876524219289422", "parent_uid": "0.8304482696112245", "expanded": false, "level": 3 } ], "uid": "0.8304482696112245", "parent_uid": "0.9416675802785903", "expanded": true, "level": 2 } ], "uid": "0.9416675802785903", "expanded": true, "level": 1 } ]
i need method receive uid params , return the specific item in params him parent. did following, got null in found:
var finditem = function(params, uid){ if (!params) return null; for(var = 0; < params.length; i++){ if(params[i].uid == uid) return params[i]; else{ finditem(params[i].children, uid); } } return null; }
example getting null: passing uid b
when examine children, should check if found something:
else { var found = finditem(params[i].children, uid); if (found !== null) return found; }
Comments
Post a Comment