循环遍历来自JIRA(Javascript)的问题对象

时间:2015-09-22 08:58:46

标签: javascript arrays object jira

这是我的代码:

jira.searchJira('priority = High', null, function(error, issuesArray) {
  if (error) {
    next(new Error(error));
  } 
  issuesObject = issuesArray[Object.keys(issuesArray)[4]];                  //All the issues
  var singleIssue = issuesObject[issuesObject[Object.keys(issuesObject)[0]]]; //gets issue at position 0 in object

  console.log(singleIssue);
  console.log(typeof(singleIssue));
});

我正在使用查询字符串(priority = High)搜索JIRA,以搜索该过滤器中的问题列表。我一直关注的文档说回调应该返回一系列问题但是,正如您所看到的,我能够Object.keys()[]返回问题(即使Array.isArray(issueArray)返回true)。< / p>

我想要实现的是循环遍历问题的数组/对象(issuesObject)以检索每个问题。 singleIssue会在0位置返回问题,但我希望有一种方法可以循环查找 ALL 问题。我尝试了for...in issuesObject,但它只是列出索引(即从0到问题数量)。

1 个答案:

答案 0 :(得分:3)

有一个回复示例here(您必须点击“展开”):

{
    "expand": "names,schema",
    "startAt": 0,
    "maxResults": 50,
    "total": 1,
    "issues": [
        {
            "expand": "",
            "id": "10001",
            "self": "http://www.example.com/jira/rest/api/2/issue/10001",
            "key": "HSP-1"
        }
    ]
}

所以,你应该能够做到以下几点:

jira.searchJira('priority = High', null, function(error, searchResults) {
   // Error handling ...
   var allIssues = searchResults.issues; // <-- this is an array.
   var singleIssue = allIssues[0]; // 0 or any other index. 
   // singleIssue is an object as well.

   // print all id's of issues:
   allIssues.forEach(function(issue) {
      console.log(issue.id); 
   });
});