如何从JSON数据索引Javascript数组?

时间:2015-09-02 06:15:34

标签: javascript json angularjs

所以我使用AngularJS制作了一个简单的JavaScript项目,

这是我的控制器

angular.module('myApp')
  .controller('BillDetail3Ctrl', function ($scope, billDetail3) { 

    $scope.tableData = [];
    $scope.hourCount = 0; 

    billDetail3.getMetList().then(function(data){    

        var no = 0;
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j == data[0].d.currA.length; j++) {
                $scope.tableData.push({
                    'No': ++no,
                    '_id': data[i]._id,
                    'hourtag': data[i].d.hourtag,  
                    'currA': data[i].d.currA[j]  
                }); 
            }
        }

        $scope.hourCount = no;
    }, function(){

    });
  });

那些控制器从这个JSON中获取数据:http://amr2.mybluemix.net/getmet/list 这是JSON的格式:

[
  {
    "_id": "100010001_20082015_0",
    "_rev": "8-e72619f2c6c05aac2e3692f1ea77eb0f",
    "d": {
      "uSN": "100010001",
      "timetag": 20082015,
      "hourtag": 0,
      "currA": {
        "0": 0,
        "1": 2,
        "2": 5,
        "3": 6,
        "4": 2.3
      },
      "currB": {
        "0": 10,
        "1": 23,
        "2": 52,
        "3": 61,
        "4": 12.3
      },
      "currABC": {
        "0": 0.1,
        "1": 2.2,
        "2": 5.2
      },

以下是我的观点:

<tr ng-repeat="pop in tableData">
       <td>{{pop.No}}</td>
       <td>{{pop._id}}</td>
       <td>{{pop.hourtag}}</td>
       <td>{{pop.currA}}</td>  //This is doesn't work
 </tr>

但是这段代码<td>{{pop.currA}}</td>不起作用。所以,在JSON中我有hourtag = 0, hourtag = 1, etc.在每个句子中,我有"currA": { "0": x, "1" = x, etc.这个想法是我想调用每个句子中第一个"0"中的所有currA值并将其放入在我的桌子上。我怎么能这样做?

以下是结论 enter image description here

2 个答案:

答案 0 :(得分:1)

它应该是这样的吗? http://jsfiddle.net/Lt7aP/1033/(简化示例)

关键更改是for for循环:

for (var i = 0; i < data.length; i++) {
    var curHourtag = data[i].d.hourtag;
    $scope.tableData.push({
        'No': ++no,
        '_id': data[i]._id,
        'hourtag': curHourtag,
        'currA': data[i].d.currA[curHourtag]
    }); 
}

答案 1 :(得分:0)

<强>解决 感谢@Dfr回答,我将'currA': data[i].d.currA[curHourtag]行修改为'currA': data[i].d.currA[0],并且按预期正常工作。

for (var i = 0; i < data.length; i++) {
    var curHourtag = data[i].d.hourtag;
    $scope.tableData.push({
        'No': ++no,
        '_id': data[i]._id,
        'hourtag': curHourtag,
        'currA': data[i].d.currA[0]
    }); 
}