While循环不读取数组对象的当前长度(角度6)

时间:2018-11-27 01:30:57

标签: angular typescript

im试图将数组拼接成多个部分,并且已经实现了我想要的功能,但是我添加了一些过滤器,仅将列表数组(lf)添加到仅具有(4)操作状态的列表数组中,而while循环无法读取数组的长度< / p>

这里是我的代码:

  lf = [];
  rows = [];
  size = 4;
  logframes$: any;

  constructor(private logframe: LogicalframeworkService) {
  }

  ngOnInit() {
    this.logframe.getLogicalFrameworks().subscribe(
      data => this.setTitle(data)
    );
  }

  setTitle(array) {
    array.forEach(item => {
      const card = {
        'id': item.id,
        'title': item.project.description
      };
      this.logframe.getAuditTrailStatusByLogFrame(item.id).subscribe(
        (data: any) => {
          if (data.logFrameActionStatusReferenceId === 4) {
            this.lf.push(card);
            console.dir(this.lf);
          }
        }
      );
    });
    while (this.lf.length > 0) {
      this.rows.push(this.lf.splice(0, this.size));
    }
  }

1 个答案:

答案 0 :(得分:0)

虽然循环代码将在“ logframe”服务返回数据之前执行。更新的代码。还有为什么需要while循环进行长度检查。您可以使用if cond来实现。

lf = [];
  rows = [];
  size = 4;
  logframes$: any;

  constructor(private logframe: LogicalframeworkService) {
  }

  ngOnInit() {
    this.logframe.getLogicalFrameworks().subscribe(
      data => this.setTitle(data)
    );
  }

  setTitle(array) {
    array.forEach(item => {
      const card = {
        'id': item.id,
        'title': item.project.description
      };
      this.logframe.getAuditTrailStatusByLogFrame(item.id).subscribe(
        (data: any) => {
          if (data.logFrameActionStatusReferenceId === 4) {
            this.lf.push(card);
            console.dir(this.lf);
            if (this.lf.length) {
              this.rows.push(this.lf.splice(0, this.size));
            }

          }
        }
      );
    });

  }