Looking to filter and include strings in an array by matching them with items in another array? Here is a sample code snippet that filters based on a single string:
const filteredString = `${this.filter}`.toLowerCase();
this.filteredCampaigns = this.filteredCampaigns.concat(this.allCampaigns.
filter((item) => item.description?.toLowerCase().includes(filteredString) ||
item.type?.toLowerCase().includes(filteredString) ||
item.code?.toLowerCase().includes(filteredString)
));
If you need to match multiple strings, split the input string into an array like this:
const filteredString: string = `${this.filter}`.toLowerCase().split(' ');
To filter based on all elements of the array together instead of individually, use a for-of loop:
for (const val of filteredString) {
this.filteredCampaigns = this.filteredCampaigns?.concat(this.allCampaigns.
filter((item) => item.description?.toLowerCase().includes(val) ||
item.type?.toLowerCase().includes(val) ||
item.code?.toLowerCase().includes(val)
));
}
}
The issue here is that each element in the array is filtered independently. If you want all items in the array to be connected in filtering, and return results that match all elements, you'll need a different approach.