In my Angular app, I am trying to display a button based on some values within the below array:
public filterList: Array<any> = [
{ 'name': 'Product', 'selected': true },
{ 'name': 'Client', 'selected': true },
{ 'name': 'Received', 'selected': false }
];
If Product is selected, and Client is selected, and Received is not selected
Then I want to display a button.
Else, I want to display an error message.
The below method performs this functionality, but it is not extensible. For example, if I update filterList with another item, it won't be checked in this method.
Instead of hard-coding the array positions, I want to loop through the array:
ngOnInit() {
this.checkSelectedFilters();
}
checkSelectedFilters() {
if (this.filterList[0].selected === true
&& this.filterList[1].selected === true
&& this.filterList[2].selected === false) {
console.log('Display button');
} else {
console.log('Display error message');
}
}
Can someone please tell me how I can update this method so that it meets my above requirement?
Note: when the app loads initially, all selected values are false & are updated by the user