如何使用angular在JSON中添加循环

时间:2016-12-19 11:51:10

标签: angularjs json

我需要创建一个像这样的JSON结构:

onreadystatechange

这是我的控制器代码,我从HTML中获取一些值,并根据我需要在JSON中迭代循环的值。我没有得到如何在JSON中添加循环,因为它是一个对象。

"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"

               }

            ]
         }
         ]
      },

   ]
}

请建议如何使用迭代循环创建JSON结构

1 个答案:

答案 0 :(得分: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
        }
      })
  }
}