1 回答
TA贡献1155条经验 获得超0个赞
您可以编写一个快速函数来更新值,如下所示
private updateGroupList(statusData: any) {
for (const key in this.groupList) {
if (this.groupList[key].vehicle_number === statusData.vehicle_number) {
this.groupList[key].status = statusData.status;
return;
}
}
this.groupList.push(statusData);
}
您可以将this.groupList.push()中的所有ngOnInit()内容替换为this.updateGroupList(this.statusdata1).
ngOnInit() {
/* First data */
this.statusdata1 = {"vehicle_number":1,"status":"red"};
this.updateGroupList(this.statusdata1);
console.log(this.groupList);
/* Second data */
setTimeout (() => {
this.statusdata1 = {"vehicle_number":1,"status":"green"};
this.updateGroupList(this.statusdata1);
console.log(this.groupList);
}, 5000);
/* Third data */
setTimeout (() => {
this.statusdata1 = {"vehicle_number":2,"status":"yellow"};
this.updateGroupList(this.statusdata1);
console.log(this.groupList);
}, 10000);
}
- 1 回答
- 0 关注
- 70 浏览
添加回答
举报