簡體   English   中英

如何使用angular獲取這些json嵌套值?

[英]How to get these json nested values using angular?

我有這個json響應:

 {
  "tags": [
    {
      "name": "SolarData",
      "results": [
        {
          "groups": [
            {
              "name": "type",
              "type": "number"
            }
          ],
          "attributes": {
            "customer": [
              "Acme"
            ],
            "host": [
              "server1"
            ]
          },
          "values": [
            [
              1429950000000,
              46,
              3
            ],
            [
              1429960000000,
              62,
              3
            ],
            [
              1429970000000,
              183,
              3
            ],
            [
              1429980000000,
              156,
              3
            ],
            [
              1429990000000,
              205,
              3
            ]
          ]
        }
      ],
      "stats": {
        "rawCount": 5
      }
    }
  ]
}

而且我希望只能獲取該項目每個值部分的前兩個項目。 敵人的例子我想在范圍變量中返回[[1429950000000,46],[1429960000000,62],[1429970000000,183],.....],以便最終可以將其用於圖形。 我一般對angular和Web開發人員是陌生的,但是到目前為止,這是我嘗試過的方法。

$http({
           url: 'file.json',
           method: 'POST',    
           data: '(query data here)'
         }).then(function(data, status){
            $scope.solarData = data.tags.results.values;
            conosle.log($scope.solarData);
        });

您可以使用map

var custom = data.tags[0].results[0].values.map(function(values) {
  return [values[0], values[1]];
});

如果要返回很多項目或可變數量的項目,可以使用slice

return values.slice(0, 2);
//---------------------^ replace this

 var data = { "tags": [{ "name": "SolarData", "results": [{ "groups": [{ "name": "type", "type": "number" }], "attributes": { "customer": [ "Acme" ], "host": [ "server1" ] }, "values": [ [ 1429950000000, 46, 3 ], [ 1429960000000, 62, 3 ], [ 1429970000000, 183, 3 ], [ 1429980000000, 156, 3 ], [ 1429990000000, 205, 3 ] ] }], "stats": { "rawCount": 5 } }] } var custom = data.tags[0].results[0].values.map(function(values) { return [values[0], values[1]]; }); console.log(custom); 

var requirementArray = new Array();
for (i = 0; i < $scope.solarData.length ; i++) {
    var pare = new Array();
    pare.push($scope.solarData[i][0]);
    pare.push($scope.solarData[i][1]);

    requirementArray.push(pare);
}

requireArray將為:

[[1429950000000,46],[1429960000000,62],[1429970000000,183],.....]

您可以Array.map使用Array.map

$http({
   url: 'file.json',
   method: 'POST',    
   data: '(query data here)'
 }).then(function(data, status){
    $scope.solarData = data.tags[0].results[0].values.map(
        function(curVal, index, arr) {
            return [curVal[0], curVal[1]];
        }
    );

    conosle.log($scope.solarData);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM