CoderBeginner
CoderBeginner

Reputation: 837

How to add a loop in JSON using angular

I need to create a JSON structure like this:

"combinationsData":[  
      {  
        "combinationName":"2_2",
        "dataGroups": [
       {  
            "tableType":"2",//This value comes from HTML
            "twoAxisData":[{
              "vAxis":18,//This value also comes from HTML
             "dataValue":[
             {//Need to iterate the hAxis. I need to add a loop to iterate this value
             "hAxis":"03",
             "dataValue":"0.7750"
               },{
             "hAxis":"04",
               "dataValue":"1.48"
               },{
             "hAxis":"05",
               "dataValue":"1.48"
               },{
             .
             .
             .
             "hAxis":"08",
               "dataValue":"0.06833"
               }
                  ] 
            },
            {
              "vAxis":20,
             "dataValue":[
             {
             "hAxis":"01",
             "dataValue":"1.48"
               },{
               "hAxis":"02",
               "dataValue":"1.48"
               },{
               "hAxis":"03",
               "dataValue":"1.48"
               },{
               "hAxis":"04",
               "dataValue":"1.48"
               },{
               "hAxis":"05",
               "dataValue":"1.48"
               },{
             "hAxis":"06",
               "dataValue":"1.48"
               },{
             "hAxis":"07",
               "dataValue":"1.48"
               }
                  ] 
            }]
         },
        {  
            "tableType":"1",
            "oneAxisData":[

               {  
                  "vAxis":"18",
                  "dataValue":"1.48"

               },
               {  
                  "vAxis":"19",
                  "dataValue":"1.48"

               },
               {  "vAxis":"19",
                  "dataValue":"1.48"

               }

            ]
         }
         ]
      },

   ]
}

This is my controller code, in which I am getting some values from HTML and based on the values I need to iterate a loop in JSON. I am not getting how to add a loop inside JSON as it is an object.

mapData = function() {
        "combinationData": {
            "combinationName": "2_2",
            "dataGroups": {
                "tableType": "2",

                "twoAxisData": {
                    for (var i = 0; i < 5; i++) { //Need to add a loop like this to iterate till its value
                        "vAxis": $scope.vAxis,
                        "dataValue": {
                            "hAxis": "2",
                            "dataValue": $scope.hrows
                        },
                    }

                }
            }

Please suggest how to create a JSON structure using a iterative loop

Upvotes: 1

Views: 46

Answers (1)

SaiNageswar S
SaiNageswar S

Reputation: 1336

mapData = function() {
  var jsonData = {
    "combinationData": {
      "combinationName": "2_2",
      "dataGroups": {
        "tableType": "2",    
        "twoAxisData": []    
      }
    }
  }
  for (var i = 0; i < 5; i++) {
    jsonData["combinationData"]["dataGroups"]["twoAxisData"]
      .push({
        "vAxis": $scope.vAxis,
        "dataValue": {
          "hAxis": "2",
          "dataValue": $scope.hrows
        }
      })
  }
}

Upvotes: 2

Related Questions