Within my application, there exists an array of objects containing a boolean property that is subject to change. Whenever this property changes, I use the .next(changedData)
method to emit the updated array to the component that is subscribed.
The component responsible for monitoring these data changes must filter the data appropriately in order to prepare it for use with *ngFor
.
I have been attempting to grasp the concept of using setter/getter methods effectively, as I wish to avoid using pipes due to potential inefficiencies when dealing with large amounts of data.
Although I came across this informative answer, I am still struggling to fully understand the proper implementation.
export class MyComponent implements OnInit {
private _listFilter: Data[];
get listFilter(): Data[] {
return this._listFilter;
}
set listFilter(filter: Data[]) {
this._listFilter = filter;
this.filteredData = this.performFilter(this.listFilter); //*ngFor is set on filteredData
}
.
.
.
ngOnInit(): void {
this.dataSub = this.dataService.DataList$.subscribe(result => {
this.data = result;
this.filteredData = this.data; //*ngFor is set on filteredData
});
}
.
.
.
private performFilter(dataArr): Data[] {
//isNewData is the propery that has changed and I want to iterate only over a list of data which the boolean isNewData equals to true
return dataArr.filter((data: Data) => data.isNewData === true );
}