简体   繁体   English

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

[英]Fetch array of object values using angular and typescript

I am trying to get the array of object values into the array using nested for loop. 我正在尝试使用嵌套的for循环将对象值的数组放入数组中。 I am getting the json data as below. 我正在获取json数据,如下所示。 For that i have written the typescript code as below. 为此,我编写了如下的打字稿代码。 But i am not able to get the desired results to bind the values to template. 但是我无法获得所需的结果来将值绑定到模板。 See below my code. 请参阅下面的代码。

This is my typescript code: 这是我的打字稿代码:

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

The below is the json data i am getting from the rest api. 以下是我从其余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:"
       },
       {}
       ]

I am trying to get the key and values of task object as below 我正在尝试获取任务对象的键和值,如下所示

task.totalTests
task.totalOpenIssues

When i am trying to get the json array of object values then i am getting this error: 当我尝试获取对象值的json数组时,我收到此错误:

ERROR TypeError: Cannot read property 'totalTestCompleted' of null

So my idea is to bind those values to template. 所以我的想法是将这些值绑定到模板。 For that i have written the everyting but i am not able to get the array of json object values. 为此,我写了所有内容,但无法获取json对象值的数组。 Can anyone help me on this? 谁可以帮我这个事?

Thanks. 谢谢。

There are some problems on your code. 您的代码上有一些问题。 According to the data you show, you don't need nested loops. 根据显示的数据,您不需要嵌套循环。 Also, there's no totalTestCompleted key anywhere in your data. 另外,数据中的任何地方都没有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);
}

Not all your elements contains the task information so you need to check for it. 并非所有元素都包含任务信息,因此您需要检查它。

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

Just another note, for (var task in totalData[i]) will return every key of the object: 还要注意一点, 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

The way you were using it you got all the keys: 您使用它的方式拥有所有的关键:

id
createdDate
inactive
job
and so on...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM