4

I am facing trouble iterating through a json object in plain javascript code. for loop or for each loop would be great but I am not able to loop it.

//I want output in format like 
{
 applicationGroupId:
 [
  treatmentIds
 ]
}

like;

{
    "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 am manually printing the result but not in for loop.

Pavan Ravipati
  • 1,890
  • 14
  • 21
Dev Singh
  • 71
  • 7
  • possible duplicate of [How do I iterate over a JSON structure?](http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure) – Epsil0neR Jul 20 '15 at 06:54
  • Not exactly, as i have already gone through this link before posting question. i have to iterate simply not key/value pair. please help. – Dev Singh Jul 20 '15 at 07:02

3 Answers3

3

As you've tried to do in your example, you can user the for-in method:

var text = '{"treatmentApplicationGroupDataList": [{"applicationGroupId": 879545457291368,"treatments":[{"treatmentId": 879545457291370}]},{"applicationGroupId": 879545447124032,"treatments": [{"treatmentId": 879545447124036 },{"treatmentId": 879545447124034}]}]}';

var myObject = JSON.parse(text);

// Using the for-in method
for (treatmentListId in myObject.treatmentApplicationGroupDataList) {
    var curTreatmentList = myObject.treatmentApplicationGroupDataList[treatmentListId];

    console.log(curTreatmentList.applicationGroupId+": ");

    for (treatmentId in curTreatmentList.treatments) {
        console.log("=>"+curTreatmentList.treatments[treatmentId].treatmentId);
    }
}    

Or you can use the .forEach method :

myObject.treatmentApplicationGroupDataList.forEach(function(treatmentList){
    console.log(treatmentList.applicationGroupId+": ");
    treatmentList.treatments.forEach(function(treatment){
        console.log("=> "+treatment.treatmentId);
    });
});

More informations available here : For-each over an array in JavaScript?

Community
  • 1
  • 1
John
  • 338
  • 2
  • 7
2

Here is a good explanation for the forEach function that you can use: https://stackoverflow.com/a/9329476/4693496

Given your following object:

{
    "treatmentApplicationGroupDataList": [
        {
            "applicationGroupId": 879545457291368,
            "treatments":
                [
                    {
                        "treatmentId": 879545457291370
                    }
                ]
        },
        {
            "applicationGroupId": 879545447124032,
            "treatments": [
                {
                    "treatmentId": 879545447124036
                },
                {
                    "treatmentId": 879545447124034
                }
            ]
        }
    ]
}

You can use the following function:

myObject.treatmentApplicationGroupDataList.forEach(function(item) {
    document.write(item.applicationGroupId);
    item.treatments.forEach(function(treatment) {
        document.write(treatment.treatmentId);
    });
});
Community
  • 1
  • 1
Erazihel
  • 7,295
  • 6
  • 30
  • 53
1

Please see the below code

var text = '{"treatmentApplicationGroupDataList": [{"applicationGroupId": 879545457291368,"treatments":[{"treatmentId": 879545457291370}]},{"applicationGroupId": 879545447124032,'+
           '"treatments": [{"treatmentId": 879545447124036 },{"treatmentId": 879545447124034}]}]}';

var myObject = JSON.parse(text);
var newObj = {};

for(var i in myObject['treatmentApplicationGroupDataList']){
    var appGroupId = myObject['treatmentApplicationGroupDataList'][i]['applicationGroupId'];
    newObj[appGroupId] = [];
    for(var j in myObject['treatmentApplicationGroupDataList'][i]['treatments'])
        newObj[appGroupId].push(myObject['treatmentApplicationGroupDataList'][i]['treatments'][j]['treatmentId'])
}

//newObj will contain the required Json.

Explaination:

The first for loop will iterate over the treatmentApplicationGroupDataList and then in that loop will get the applicationGroupId for this list and make a new Object for it with its key as the applicationGroupId with a value as an array The next nested for loop will loop through all the treatments in that group to extract the treatmentId and to push them in the array created in the above for loop.

sahil gupta
  • 2,339
  • 12
  • 14