1

I'm trying to loop my child array but it's not working. It's working fine which it's first items but not working on the child elements.

"candidateApplication": [
    {
        "label": "Are you currently attending school?",
        "id": "ap_currentschool",
        "value": "",
        "required": "N",
        "type": "radio",
        "display": null,
        "list": [
            {
                "searchCode": "yes",
                "searchValue": "yes"
            },
            {
                "searchCode": "no",
                "searchValue": "no"
            }
        ],
        "onSelect": [
            {
                "yes": [
                    {
                        "label": "Estimated Completion Date",
                        "id": "ap_compdate",
                        "value": "",
                        "required": "N",
                        "type": "date",
                        "display": null,
                        "list": null,
                        "onSelect": null
                    }
                ],
                "no": null
            }
        ]
    },
]

I am easily access my data to looping lists like obj.candidateApplication.list But I am not able to loop through obj.candidateApplication.onSelect.yes.

Can anybody please guide me how can I fix that issue. enter image description here

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Welcome to SO. You might find reading the site [help section](https://stackoverflow.com/help) useful when it comes to [asking a good question](https://stackoverflow.com/help/how-to-ask), and this [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Code that you've worked on to solve the problem should include a [mcve], and be included in your question. – Andy Aug 25 '21 at 16:19
  • JSON is a text format. By the time you're doing things like `obj.candidateApplication`, it's an object, not JSON. – Heretic Monkey Aug 25 '21 at 16:23
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Heretic Monkey Aug 25 '21 at 16:24
  • `obj.candidateApplication` is an array (`[...]`) so you need to use an index to get the object you want under it (e.g., `obj.candidateApplication[0].list` or `obj.candidateApplication[0].onSelect[0].yes`), or iterate over `obj.candidateApplication` – Heretic Monkey Aug 25 '21 at 16:28

1 Answers1

0

You are accessing obj.candidateApplication.onSelect.yes which is undefined because obj.candidateApplication.onSelect is an array of objects.

You probably want to loop over obj.candidateApplication.onSelect and then for each object you can access the yes property as you wish:

$.each(onSelect, function (index, element) {
    $.each(element.yes, function (x, y) {
        // your code here
    })
});

Edit: As pointed by Heritic Monkey in the comments, obj.candidateApplication is an array as well, but in the picture attached to the question, there's already a local variable called onSelect, loop through that instead of obj.candidateApplication.onSelect because it's undefined. (at least it should be, we don't know what your code is doing)

soufiane yakoubi
  • 861
  • 11
  • 31