Json object iteration

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.

//I want output in format like 
{
 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.

+4
source share
3 answers

As you tried in your example, you can use 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 . forEach method :

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

Additional information is available here: For each array in JavaScript?

+3
source

Here is a good explanation of the forEach function you can use: fooobar.com/questions/196 / ...

Given the following object:

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

:

myObject.treatmentApplicationGroupDataList.forEach(function(item) {
    document.write(item.applicationGroupId);
    item.treatments.forEach(function(treatment) {
        document.write(treatment.treatmentId);
    });
});
+2

,

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 loop forwill iterate over treatmentApplicationGroupDataList, and then in this loop it will get applicationGroupIdfor this list and create a new object for it with it keyas applicationGroupIdwith valueas arrayThe next nested loop forwill go through everything treatmentsin this group to extract treatmentIdand click them arrayin the loop created above.

+1
source

Source: https://habr.com/ru/post/1598646/


All Articles