使用angular和Typescript获取对象值数组

时间:2018-09-25 13:32:36

标签: javascript arrays angular typescript

我正在尝试使用嵌套的for循环将对象值的数组放入数组。我正在获取json数据,如下所示。为此,我编写了如下的打字稿代码。但是我无法获得所需的结果来将值绑定到模板。参见下面的代码。

这是我的打字稿代码:

let totalData = results['data'];
     for (var i=0; i<totalData.length; i++)
        for (var task in totalData[i]) {
            console.log("Task: "+task);
            console.log("totalTests: "+task['totalTestCompleted']);
            console.log("totalOpenIssues: "+totalData[i][task].totalOpenIssues);
        }

以下是我从其余api获取的json数据。

   [
    {  
          "id":"8a8080f06610359c0166103cf22c7032",
          "createdDate":"2018-09-25T10:18:41.580+0000",
          "inactive":false,
          "job":{  
             "id":"8a80809465ebb03f0165ebf9a780009b",
             "createdDate":"2018-09-18T09:18:51.775+0000",
             "inactive":false,
             "project":{  
                "id":"8a8080a565e5c6f90165e5dc7d950089",
                "createdDate":"2018-09-17T04:49:17.205+0000",
                "inactive":false,
                "name":"testproject917",
                "org":{  
                   "id":"8a8080cf65e02c0f0165e031fb9e0003",
                   "createdBy":"anonymousUser",
                   "createdDate":"2018-09-16T02:24:56.734+0000",
                   "inactive":false,
                   "name":"Google"
                }
             },
             "name":"prod",
             "refId":"prod",
             "description":null,
             "environment":{  
                "id":"8a80809465ebb03f0165ebf9a779009a",
                "createdDate":"2018-09-18T09:18:51.769+0000",
                "inactive":false,
                "baseUrl":" http://www.google.com",
                "auths":[  
                   {  
                      "name":"prodenv",
                      "authType":"Basic",
                      "username":"Google//admin@google.io",
                      "password":"4+Coma/w5kOSlofdJLeBT6b4tdGNrVbE"
                   }
                ]
             },
             "issueTracker":{  
                "id":"8a80809465ebb03f0165ebf9a780009c",
                "name":"Dev-IssueTracker"
             },
             "notifications":[  
                {  
                   "id":12,
                   "name":"Dev-Email-Notification"
                },
                {  
                   "id":null,
                   "name":"Default_Slack"
                }
             ],
             "categories":"",
             "notificationToDo":false,
             "issueTrackerToDo":false,
             "openIssues":0,
             "closedIssues":0
          },
          "runId":9,
          "task":{  
             "name":"Tue Sep 25 10:18:41 UTC 2018",
             "description":"Invalid Region: US_WEST_1",
             "status":"FAIL",
             "totalTests":0,
             "totalOpenIssues":0
          },
          "attributes":{  
             "REGION":"US_WEST_1"
          },
          "regions":"US_WEST_1",
          "stats":{  

          },
          "validations":null,
          "ciCdStatus":"FAIL:Invalid Region: US_WEST_1FAIL:0.0:0:0:"
       },
       {}
       ]

我正在尝试获取以下任务对象的键和值

task.totalTests
task.totalOpenIssues

当我尝试获取对象值的json数组时,我遇到此错误:

ERROR TypeError: Cannot read property 'totalTestCompleted' of null

所以我的想法是将这些值绑定到模板。为此,我写了所有内容,但无法获取json对象值的数组。有人可以帮我吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题。根据显示的数据,您不需要嵌套循环。另外,数据中的任何地方都没有totalTestCompleted键。

let totalData = results['data'];
for (var i=0; i<totalData.length; i++) {
    // totalTestCompleted doesn't exist: console.log("totalTests: "+task.totalTestCompleted);
    console.log("totalOpenIssues: "+ totalData[i].task.totalOpenIssues);
}

答案 1 :(得分:1)

并非所有元素都包含任务信息,因此您需要检查它。

for (var key in totalData) {
  console.log('Task id: ' + key);
  // You have an empty item in the end
  if (totalData[key].task) {
        console.log('Total tests: ' + totalData[key].task.totalTests);
    console.log('Total open issues: ' + totalData[key].task.totalOpenIssues);
  }
  else {
    console.log('No task');
  }
}

请注意,for (var task in totalData[i])将返回对象的每个键:

var o = { a: 1, b: 2 };
for (var k in o) {
  console.log('Key: ' + k);
}

// The above will log
Key: a
Key: b

您使用它的方式拥有所有的按键:

id
createdDate
inactive
job
and so on...