Currently, I am working with an array that requires me to filter out specific keys without looping through them. I have created separate filters for each key needed:
this.filteredCampaigns = this.allCampaigns.filter(
(item) => item.status?.includes(translatedStateName)
);
this.filteredCampaigns = this.filteredCampaigns.concat(this.allCampaigns.filter(
(item) => item.description?.toLowerCase().includes(filteredString)
));
this.filteredCampaigns = this.filteredCampaigns.concat(this.allCampaigns.filter(
(item) => item.type?.toLowerCase().includes(filteredString))
);
this.filteredCampaigns = this.filteredCampaigns.concat(this.allCampaigns.filter(
(item) => item.code?.toLowerCase().includes(filteredString))
);
I am looking for a way to combine or chain these filters instead of repeating the code. I have attempted:
this.filteredCampaigns = this.filteredCampaigns.concat(this.allCampaigns.filter(
(item) => item.description?.toLowerCase().includes(filteredString)
&& item.type?.toLowerCase().includes(filteredString)
&& item.code?.toLowerCase().includes(filteredString)
));
Unfortunately, this approach does not work. Can someone provide guidance on how to achieve this?
Edit: Thank you for the comments, I realized that to combine filters, I needed to replace &&
with ||
.