filter - Filtering for Nested Property in Javascript/Lodash -


i have data looks this:

[{     "id": 1,     "name": "canada",     "checked": true,     "vacationspots": [{         "id": 1,         "name": "toronto",         "checked":false,         "activities": [{             "id": 1,             "checked": false,             "name": "niagara falls"         }]     }, {         "id": 2,         "name": "france",         "checked":true,         "activities": [{             "id": 2,             "checked":true,             "name": "eiffel tower"         }]     }] }, {     "id": 2,     "name": "us",     "checked": true,     "vacationspots": [{         "id": 3,         "name": "california",         "checked": true,         "activities": [{             "id": 3,             "name": "surfing",             "checked":false         }]     }] }] 

i'm gathering activities id's activities have checked set true.

so result looks this:

2 

while can this, have go through 3 levels before can access activities

  (i = 0; < country.length; i++){     country = allareas[i];   ....     (j = 0; j < country.vacationspots.length; j++){   ....         (k = 0; k < vacationspots.activities.length; k++){ 

(search through country, vacationspots, activities. there way filter without traversing through each level? there way lodash?

in interest of providing array of unique activity ids checked activities across entire data set, assuming particular activity potentially show in more 1 country / vacation spot, should suffice

let data = [{"id":1,"name":"canada","checked":true,"vacationspots":[{"id":1,"name":"toronto","checked":false,"activities":[{"id":1,"checked":false,"name":"niagara falls"}]},{"id":2,"name":"france","checked":true,"activities":[{"id":2,"checked":true,"name":"eiffel tower"}]}]},{"id":2,"name":"us","checked":true,"vacationspots":[{"id":3,"name":"california","checked":true,"activities":[{"id":3,"name":"surfing","checked":false}]}]}];    let activitymap = data.reduce((map, country) => {      country.vacationspots.foreach(vs => {          vs.activities.foreach(activity => {              if (activity.checked) {                  map[activity.id] = true;              }          });      });      return map;  }, object.create(null));    let activities = object.keys(activitymap).map(number);  console.log(activities);


Comments