如何减少重复的线条?

时间:2018-10-24 10:26:52

标签: javascript angular typescript

下面的函数可以执行预期的操作,但是代码会重复很多次。

有什么方法可以简化下面的代码?

  getJiraOpenIssues(id:string) {
    return this.http.get<any>( apiUrl )
    .subscribe(response => {
      let open = response.issues.filter((status) => {
        return status.fields.status.name === 'Open';
      });

      let closed = response.issues.filter((status) => {
        return status.fields.status.name === 'Closed';
      });

      let resolved = response.issues.filter((status) => {
        return status.fields.status.name === 'Resolved';
      });

      let progress = response.issues.filter((status) => {
        return status.fields.status.name === 'In Progress';
      });

      let Total = open.length + closed.length + resolved.length + progress.length;
      console.log(Total);

    });
  }

3 个答案:

答案 0 :(得分:2)

您可以这样做。

   getJiraOpenIssues(id:string) {
        const desired = ['Open', 'Closed', 'Resolved', 'In Progress'];
        return this.http.get<any>( apiUrl )
        .subscribe(response => {
          let items = response.issues.filter((status) => {
            return desired.includes(status.fields.status.name);
          });

          console.log(item.length);

        });
      }

编辑 建议:

使用过JIRA之后,我可以考虑使用JIRA JQL API和下面的文档网址。

我想必须有一个API可以传递给我们,而我们需要您真正需要的问题状态,而不是最后进行过滤。此外,JQL在数据库上执行,它将更有效地执行过滤工作。

https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/

答案 1 :(得分:0)

也许类似

getJiraOpenIssues(id:string) {
  const fn = (response, state) => {
    return response.issues.filter((status) => status.fields.status.name === state);
  };

  return this.http.get<any>( apiUrl )
  .subscribe(response => {
    let open = fn(response, 'Open');
    let closed = fn(response, 'Closed');
    let resolved = fn(response, 'Resolved');
    let progress = fn(response, 'In Progress');

    let Total = open.length + closed.length + resolved.length + progress.length;
    console.log(Total);
  });
}

答案 2 :(得分:0)

getJiraOpenIssues(id:string) {
    return this.http.get<any>( apiUrl )
    .subscribe(response => {
        let open = this.responseFilter(response, 'Open');

        let closed = this.responseFilter(response, 'Closed');

        let resolved = this.responseFilter(response, 'Resolved');

        let progress = this.responseFilter(response, 'In Progress');

        let Total = open.length + closed.length + resolved.length + progress.length;
        console.log(Total);

    });
}

responseFilter (response, filter) {
    fv = response.issues.filter((status) => {
        return status.fields.status.name === filter;
    });
    return fv;
}