javascript - Underscore.js : matching string in deeply nested objects -


i need find out object in deep nested array contains string toys & games (the deepest object in nest).

i trying select right object with:

var arr = _.filter(data, function(obj) {     return _.some(obj.ancestors, {'name': "toys & games"}); }); 

where data being passed in .filter is:

 {       "browsenodeid":[          "11608080011"     ],     "name":[          "quadcopters & multirotors"     ],     "ancestors":[          {             "browsenode":[                {                   "browsenodeid":[                      "166583011"                 ],                 "name":[                      "rc vehicles"                 ],                 "ancestors":[                      {                         "browsenode":[                            {                               "browsenodeid":[                                  "6925830011"                             ],                             "name":[                                  "rc vehicles & parts"                             ],                             "ancestors":[                                  {                                     "browsenode":[                                        {                                           "browsenodeid":[                                              "276729011"                                         ],                                         "name":[                                              "hobbies"                                         ],                                         "ancestors":[                                              {                                                 "browsenode":[                                                    {                                                       "browsenodeid":[                                                          "165795011"                                                     ],                                                     "name":[                                                          "categories"                                                     ],                                                     "iscategoryroot":[                                                          "1"                                                     ],                                                     "ancestors":[                                                          {                                                             "browsenode":[                                                                {                                                                   "browsenodeid":[                                                                      "165793011"                                                                 ],                                                                 "name":[                                                                      "toys & games"                                                                 ]                                                              }                                                           ]                                                        }                                                     ]                                                  }                                               ]                                            }                                         ]                                      }                                   ]                                }                             ]                          }                       ]                    }                 ]              }           ]        }     ]  },  {       "browsenodeid":[          "3226142011"     ],     "name":[          "grown-up toys"     ],     "children":[          {             "browsenode":[                {                   "browsenodeid":[                      "3226143011"                 ],                 "name":[                      "action & toy figures"                 ]              },              {                   "browsenodeid":[                      "3226145011"                 ],                 "name":[                      "die-cast & toy vehicles"                 ]              },              {                   "browsenodeid":[                      "3226146011"                 ],                 "name":[                      "games"                 ]              },              {                   "browsenodeid":[                      "3226148011"                 ],                 "name":[                      "novelty & gag toys"                 ]              },              {                   "browsenodeid":[                      "3226149011"                 ],                 "name":[                      "puzzles"                 ]              },              {                   "browsenodeid":[                      "5483953011"                 ],                 "name":[                      "executive desk toys"                 ]              }           ]        }     ],     "ancestors":[          {             "browsenode":[                {                   "browsenodeid":[                      "165795011"                 ],                 "name":[                      "categories"                 ],                 "iscategoryroot":[                      "1"                 ],                 "ancestors":[                      {                         "browsenode":[                            {                               "browsenodeid":[                                  "165793011"                             ],                             "name":[                                  "toys & games"                             ]                          }                       ]                    }                 ]              }           ]        }     ]  } 

i ended doing without underscore, async while loop instead:

function traversenodes(nodelist,findme,callbackmm){      var nodearr = []; //collect browse nodes      async.eachseries(nodelist, function(item, callbackz) {          var currentnode = item;         var childnodeid = currentnode.browsenodeid[0]; //get first id in nest tree          async.whilst(             function () {                  if (currentnode.name && currentnode.name[0] == findme){ //we found string, stop traversing                     nodearr.push(childnodeid);                     return false;                 }else if (currentnode.ancestors){ //didn't find string, keep traversing                     currentnode = currentnode.ancestors[0].browsenode[0];                     return true;                 }                 else {                     return false;                 }             },             function (callback) {                 callback();                                                                             },             function (err) {                 if (err){                     console.log('whilst error in search.js ',err);                 }                 callbackz();             }         );           }, function done(){         if (nodearr.length > 0){                                                                 callbackmm(nodearr.tostring());          }         else {             console.log('error: no browsenodes found')         }                                               });                                    } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -