Sari Rahal
Sari Rahal

Reputation: 1955

Angular looping a array of JSONs

I currently have an array of JSONs that I am trying to print out to the screen by using a loop. Currently I can get my results I want without the loop but when I try to convert it to a loop I run into some issues. Any insight would be helpful. Thank you.

This is my current code:

<div>
    <h2>Document Type</h2>
    <select>
        <option>Document Type : {{handoff[0].documentType}}</option>
        <option>Document Type : {{handoff[1].documentType}}</option>
        <option>Document Type : {{handoff[2].documentType}}</option>
        <option>Document Type : {{handoff[3].documentType}}</option>
        <option>Document Type : {{handoff[4].documentType}}</option>
    </select>
    <select>
        <option ng-repeat="temp in handoff">Document Type : {{temp[0].documentType}}</option>
    </select>
    <select>
        <option ng-repeat="temp in handoff">Document Type : {{temp.documentType}}</option>
    </select>
</div>

My results for the first "select" are correct but for the second and third have no results.

Here is what the obj looks like: enter image description here

"handoffs":{"0":{   "documentType":"0","conlID":"1230","userName":"CU0","handoff":"h=81878E9E772BCA176D868CA633BFB47D38274B1B209FD80856E56B47"
},"1":{"documentType":"1","conlID":"C010","userName":"A010","handoff":"ERROR: A temporary problem was encountered.  Please try your request again in a few minutes."},"3":{"documentType":"3","conlID":"C010","userName":"C10","handoff":"HANDOFF=81878E9E77FB56E56B47"}}

Upvotes: 0

Views: 69

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

You should correct you json structure & shouldn't have "0", "1" & "2" in object literals in your response, You could simply have an array so that your problem would get solved.

Updated JSON

{
    "handoffs": [{
            "documentType": "0",
            "conlID": "1230",
            "userName": "CU0",
            "handoff": "h=81878E9E772BCA176D868CA633BFB47D38274B1B209FD80856E56B47"
        },{
            "documentType": "1",
            "conlID": "C010",
            "userName": "A010",
            "handoff": "ERROR: A temporary problem was encountered.  Please try your request again in a few minutes."
        },{
            "documentType": "3",
            "conlID": "C010",
            "userName": "C10",
            "handoff": "HANDOFF=81878E9E77FB56E56B47"
        }]
    }
}

Update

@o4ohel suggested a good option, if you don't have any control over the data & don't wanted to change it. Then you should use the option

<select>
    <option ng-repeat="(key, value) in handoff">Document Type : {{value.documentType}}</option>
</select>

Upvotes: 1

Related Questions