{
'lastChangedName': null'Caption': 'Adhesive Tape',
'CreateByID': 0,
'DateOnSet': '02/10/2011',
'Note': 'Currently participating in Allergy shots to resolve this',
'Reaction': 'skin rash',
'SectionDescription': 'Allergy List',
'HistoryItemID': 1831,
'CurrentInDrFirst': 0,
'CreateDate': '/Date(1297674300000-0500)/',
'Code': '3746',
'PL': '1',
'Problem': {
"LastChargedByUserID": 0,
"LastChargedDate": null,
"ProblemStatus": 1003,
"DateResolved": "12\/2\/11",
"PatientID": 0,
"ProblemID": 1330
},
'CategoryDescription': null,
'CategoryID': 0,
'CodeSystem': 'FDBDRUG',
'SectionID': 3,
'LastChangedID': 0,
},
{
'lastChangedName': null'Caption': 'Cats',
'CreateByID': 0,
'DateOnSet': '6/4/1997',
'Note': '0',
'Reaction': 'Sneezing',
'SectionDescription': 'Allergy List',
'HistoryItemID': 1925,
'CurrentInDrFirst': 0,
'CreateDate': '/Date(1299176220000-0500)/',
'Code': '',
'PL': '1',
'Problem': {
"LastChargedByUserID": 0,
"LastChargedDate": null,
"ProblemStatus": 1002,
"DateResolved": null,
"PatientID": 0,
"ProblemID": 1331
},
'CategoryDescription': null,
'CategoryID': 0,
'CodeSystem': '',
'SectionID': 3,
'LastChangedID': 0,
}
This is the response i get, i want to iterate and print out the caption values… in a text box…
for each(var item in response) {
alert(item.caption)
}
This only prints me the first caption alone.
That response, as quoted, is awkward because you have a series of anonymous objects separated by commas (you might think it was invalid, but it’s not, it’s just unhelpful). (That’s assuming you fix the
'lastChangedName': null'Caption'thing that symcbean pointed out in the comments; I assume that’s a copy-and-paste error.) I think there must be more to it than what’s quoted. (Is it, perhaps, inside[and], making it an array of objects?)But generally speaking: Yes,
for..in(notfor each, justfor) is used to loop through the names of the properties of an object, and once you have each name, you can use[]notation to retrieve the property value.So if you can get a reference to each of those objects, you can use
for..into loop through the properties.Example:
That will show
or
The order of the property names in the loop is not specified for generic objects like those in your example. (It’s specified for
Arrayobjects: WithArrayobjects it’s guaranteed that any properties they have with purely numeric names — e.g., array indexes — will be iterated in numeric order. [Even then, if you have non-numeric properties on the array as well, it’s undefined whether those will be before, after, or intermixed with, the numeric ones.])