Recursion maintaining ancestors/parents nested object in JavaScript -


i have deep nested category structure , given category object can exist @ depth. need able iterate through category nodes until find requested category, plus able capture parent categories way through.

data structure

[ {     categoryname: 'antiques' }, {     categoryname: 'art',     children: [         {             categoryname: 'digital',             children: [                 {                     categoryname: 'nesting..'                 }             ]         },         {             categoryname: 'print'         }     ] }, {     categoryname: 'baby',     children: [         {             categoryname: 'toys'         },         {             categoryname: 'safety',             children: [                 {                     categoryname: 'gates'                 }             ]         }     ] }, {     categoryname: 'books' } 

]

code in place

function findcategoryparent (categories, category, result) {     // iterate through our categories...initially passes in root categories     (var = 0; < categories.length; i++) {          // check if our current category 1 looking         if(categories[i] != category){             if(!categories[i].children)                 continue;              // want store each ancestor in result array             var result = result || [];             result.push(categories[i]);             // since want return data, need return our recursion             return findcategoryparent(categories[i].children, category, result);         }else{             // in case user clicks parent category , doesnt hit above logic             if(categories[i].categorylevel == 1)                 result = [];              // woohoo...we found             result.push(categories[i]);             return result;         }     } } 

problem

  1. if return recursive function work fine 'art' , of children..but since returns, category baby never gets hit , therefor never find 'gates' lives baby/safety/gates

  2. if not return recursive function can return root level nodes

would appreciate recommendations or suggestions.

alright, believe found solution appears work , not sure why brain took long figure out...but solution of course closure.

essentially use closure keep scoped recursion , maintain each iteration has traveled through

var someobj = {     find: function (category, tree, path, callback) {         var self = this;         (var = tree.length - 1; >= 0; i--) {              // closure allow scope our path variable , have traversed             // in our initial , subsequent closure functions             (function(){                 // copy not reference                 var currentpath = path.slice();                  if(tree[i] == category){                     currentpath.push({name: tree[i].name, id: tree[i].id});                     var obj = {                         index: i,                         category: category,                         parent: tree,                         path: currentpath                     };                     callback(obj);                 }else{                     if(tree[i].children){                         currentpath.push({name: tree[i].name, id: tree[i].id});                         self.find(category, tree[i].children, currentpath, callback);                     }                 }              })(tree[i]);         }     },      /**      * gets called when user clicks category remove      * @param  {[type]} category [description]      * @return {[type]}          [description]      */     removecategory: function (category) {         // starts quest our category , ancestors         // category 1 want         // this.list our root list of categoires,         // pass in intial empty array, each closure add own instance         // callback finish things off         this.find(category, this.list, [], function(data){             console.log(data);         });     } } 

hope helps others need way traverse javascript objects , maintain parent ancestors.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -