2016-12-19 2 views
0

ich eine JSON-Struktur wie folgt erstellen müssen:Wie eine Schleife in JSON hinzufügen mit Winkel

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

       } 

      ] 
     } 
     ] 
     }, 

    ] 
} 

Dies ist mein Controller-Code, in dem ich einige Werte von HTML bin immer und basierend auf den Werten I müssen eine Schleife in JSON durchlaufen. Ich bekomme nicht, wie man eine Schleife in JSON hinzufügt, da es ein Objekt ist.

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 
         }, 
        } 

       } 
      } 

Bitte vorschlagen, wie ein JSON-Struktur unter Verwendung einer iterativen Schleife

+0

dass Iterator außerhalb des 'JSON' erstellen und das Ergebnis in den' JSON' Schlüssel –

+0

Können Sie bitte Ihre UI-Ansicht ? –

Antwort

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