lost9123193
lost9123193

Reputation: 11030

Filtering for Nested Property in Javascript/Lodash

I currently have data that looks like 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 the activities id's from activities which have checked set to true.

so the result looks something like this:

2

While I can get this, I have to go through 3 levels before I can access activities

  for (i = 0; i < country.length; i++){
    country = allAreas[i];
  ....
    for (j = 0; j < country.vacationSpots.length; j++){
  ....
        for (k = 0; k < vacationSpots.activities.length; k++){

(Search through country, then vacationSpots, then activities. Is there a way to filter this without traversing through each level? Is there a way to do this with Lodash?

Upvotes: 0

Views: 119

Answers (1)

Phil
Phil

Reputation: 164767

In the interest of providing an array of unique activity IDs for checked activities across your entire data set, assuming that any particular activity could potentially show up in more than one country / vacation spot, something like this 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);

Upvotes: 2

Related Questions