0

I'm trying to iterate through a nested array object like below. What is the best way to access each of the object elements within the nested arrays.

{
    "titleId": "111G",
    "aNumber": "1212",
    "data": [{
            "id": "6657",
            "name": "test name",
            "city": "LA",
            "state": "CA",
            "comment": "comment 1",
            "dates": [{
                    "startDate": "01/17/2020",
                    "endDate": "01/22/2020"
                },
                {
                    "startDate": "01/24/2020",
                    "endDate": "01/30/2020"
                }
            ]
        },
        {
            "id": "123",
            "name": "abc",
            "city": "NJ",
            "state": "NY",
            "comment": "comment 2",
            "dates": [{
                    "startDate": "01/17/2020",
                    "endDate": "01/22/2020"
                },
                {
                    "startDate": "01/24/2020",
                    "endDate": "01/30/2020"
                }
            ]
        }
    ]
}

I need to access each of the elements in data and the dates array as well

1
  • 2
    Please post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Apr 13, 2020 at 4:38

2 Answers 2

1

if I understand the question correctly you want to iterate over the dates array inside of each item in the data item, this is how I would do it in js

var date = JSON.parse(res.data)

date.forEach(element => {
    var items =  element.dates
    items.forEach(current => {
        //do whatever 
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
    const info = {
    "titleId": "111G",
    "aNumber": "1212",
    "data": [{
            "id": "6657",
            "name": "test name",
            "city": "LA",
            "state": "CA",
            "comment": "comment 1",
            "dates": [{
                    "startDate": "01/17/2020",
                    "endDate": "01/22/2020"
                },
                {
                    "startDate": "01/24/2020",
                    "endDate": "01/30/2020"
                }
            ]
        },
        {
            "id": "123",
            "name": "abc",
            "city": "NJ",
            "state": "NY",
            "comment": "comment 2",
            "dates": [{
                    "startDate": "01/17/2020",
                    "endDate": "01/22/2020"
                },
                {
                    "startDate": "01/24/2020",
                    "endDate": "01/30/2020"
                }
            ]
        }
    ]
}
info.data.forEach(city => city.dates.forEach(cityDate => console.log(cityDate.startDate)))

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.