2021年4月5日星期一

json filter nested array with javascript

I want to pull with javascript: {"subNav0", "subNav1", "subNav2", "subNav3", "subNav4", "subNav5"}.

my json:

 var data = {     "menus":{        "GrandparentNav0":{           "name":"TopNav",           "items":[              {                 "name":"ParentNav0",                 "iconClass":"",                 "items":[                    {                       "name":"ParentNav1",                       "iconClass":"",                       "items":[                          {                             "name":"subNav0",                             "iconClass":""                          },                          {                             "name":"subNav1",                             "iconClass":""                          },                          {                             "name":"subNav2",                             "iconClass":""                          },                          {                             "name":"subNav3",                             "iconClass":""                          },                          {                             "name":"subNav4",                             "iconClass":""                          },                          {                             "name":"subNav5",                             "iconClass":""                          }                       ]                    },                                     ]              }           ]        }     },           };  

i know basic filter of an array:

data .forEach(function(o) {   o.variable = o.variable.filter(s => s.value == value);   });  

I dont know how to get through menus, GrandparentNav0 to pull the subNav(s)

https://stackoverflow.com/questions/66891645/json-filter-nested-array-with-javascript April 01, 2021 at 12:26AM

没有评论:

发表评论