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.

2
  • possible duplicate of How do I iterate over a JSON structure? Commented Jul 20, 2015 at 6: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. Commented Jul 20, 2015 at 7:02

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

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);
    });
});

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.