1

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

2
  • Use that iterator outside the JSON and add the result to JSON key Commented Dec 19, 2016 at 11:54
  • Can you please add your UI view ? Commented Dec 19, 2016 at 12:01

1 Answer 1

2
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
        }
      })
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.