Currently i´m working in application where i need to filter an array of items, the filters need to be persistent, i save the filters and when i get the filters back i need to check which filters are active or not and change the check state of the checkbox.
for this im using a function existInArray(element, array): boolean that check is the filter is in the array of filters and return true or false
Check is filter is active
public existInArray(element, array: Array<string>): boolean {
if (!element) {
return;
}
return array.indexOf(element.toString()) > -1;
}
Checkbox template
<div class="filter-list-container m-top">
<div *ngFor="let assignment of (filterService.getFilter(filterType.FILTER_ASSIGNMENT).items.ToArray() | filterQuery: filterOptions.assignments | sortBy: configService.config.sortUserBy:'assignment')">
<mat-checkbox [checked]="existInArray(assignment.id, filterService.filterAssignment)"
[ngClass]="{ 'checkbox-ie': platform.TRIDENT }"
(change)="filterBy(filterType.FILTER_ASSIGNMENT, assignment.id, filterService.filterAssignment, assignment.name)"
[value]="assignment.id">
<div class="assigned">
<user-image [inline]="true" [user]="assignment.assignment"></user-image>
<p class="assigned-name">{{ assignment.name }}</p>
</div>
</mat-checkbox>
</div>
</div>
Now the function is working just fine, there is no error, but there is problem here, and is because of the angular change detection, the function is been call more than 1000 times, so i was thinking in maybe use a pipe but for some reason it did not work, any idea in which other way can i do this or maybe a way to optimize ( just in case i´m using angular material for the checkbox components ).

Array.prototype.includesinstead of checking ifArray.prototype.indexOfis more than-1. - Edricassignment, e.g.assignment.__checked__ = existInArray(....)computed only when underlying data changes. and in html,[checked]="assignment.__checked__ "- ABOS