0

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?

9
  • Don't see a way to do this without using nested iterators. What does your current code look like? Commented Sep 2, 2016 at 1:29
  • @Phil The way I'm currently traversing through this is via three for loops, 1 to loop over each country, another to loop over the vacationSpots in each country and a final 1 to loop over the activities in each vacationspot. Commented Sep 2, 2016 at 2:06
  • I updated my code with a bit of my for loop snippet Commented Sep 2, 2016 at 2:12
  • 1
    If you want to go through an array to find something, you have to go through the array. If you want to go through a nested array to find something, you have to go through the nested array--one way or another. Commented Sep 2, 2016 at 3:38
  • can activities contain more than one element? Commented Sep 2, 2016 at 3:54

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.