I ran into the problem of iterating through a json object in plain javascript code. for a loop or for each loop will be fine, but I can't loop it.
{
applicationGroupId:
[
treatmentIds
]
}
as
{
"879545457291368": [
879545457291370
],
"879545447124032": [
879545447124036,
879545447124034
]
}
<script>
var text = '{"treatmentApplicationGroupDataList": [{"applicationGroupId": 879545457291368,"treatments":[{"treatmentId": 879545457291370}]},{"applicationGroupId": 879545447124032,'+
'"treatments": [{"treatmentId": 879545447124036 },{"treatmentId": 879545447124034}]}]}';
var myObject = JSON.parse(text);
for(var i in myObject){
document.write(myObject.treatmentApplicationGroupDataList[0].applicationGroupId);
document.write(myObject.treatmentApplicationGroupDataList[0].treatments[0].treatmentId);
document.write(myObject.treatmentApplicationGroupDataList[1].applicationGroupId);
document.write(myObject.treatmentApplicationGroupDataList[1].treatments[0].treatmentId);
ocument.write(myObject.treatmentApplicationGroupDataList[1].treatments[1].treatmentId);
}
</script>
So, as you can see, I manually print the result, but not in the for loop.
source
share