I have some objects, that I am receiving one after another using settimeout
function and then pushing every time into array to populate into a table. That is coming dynamically in my project but just for reference I am using settimeout
and hard coding here.
Now my problem is, whenever I am receiving data using settimeout
, I need to get last object by filtering with vehicle_number (if it contains same vehicle_number, I need to get last object of that vehicle_number) and need to populate/push into table again. Here is the code I have tried.
home.component.html
<div>
<table>
<tr *ngFor="let x of groupList">
<td>{{x.vehicle_number}}</td>
<td>{{x.status}}</td>
</tr>
</table>
</div>
home.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
imageSource :any;
statusdata1: any;
vehicle_number:any;
groupList:any = [];
constructor() {}
ngOnInit() {
/* First data */
this.statusdata1 = {"vehicle_number":1,"status":"red"};
this.groupList.push(this.statusdata1);
console.log(this.groupList);
/* second data */
setTimeout (() => {
this.statusdata1 = {"vehicle_number":1,"status":"green"};
this.groupList.push(this.statusdata1);
console.log(this.groupList);
}, 5000);
/* Third data */
setTimeout (() => {
this.statusdata1 = {"vehicle_number":2,"status":"yellow"};
this.groupList.push(this.statusdata1);
console.log(this.groupList);
}, 10000);
}
}