而循环不读取阵列对象的当前长度(角度6)



im试图将数组拼接成多个部分,并且已经达到了我想要的效果,但我添加了一些过滤器,只添加到只有(4(个操作状态的列表数组(lf(中,while循环无法读取数组的长度

这是我的代码:

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

While循环代码将在"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));
}
}
}
);
});
} 

最新更新