Looking to dynamically filter data based on various interfaces? Let's explore how to achieve this flexibility:
export interface Country {
cId: string;
cName: string;
cFlag: string;
}
export interface Address {
aId: string;
aName: string;
aStreet: string;
}
An existing method filters data from one type of interface, but we aim to make it versatile for any interface. Here's the updated code snippet:
public data: any[] = [];
var filteredObject = this.data;
var { search } = query;
if (search) {
search = search.toLowerCase();
filteredObject = filteredObject.filter((f) => {
f.includes(search);
({ firstColumn, secondColumn, thirdColumn }) =>
firstColumn.toLowerCase().includes(search) ||
secondColumn.toLowerCase().includes(search) ||
thirdColumn.toLowerCase().includes(search)
});
}
The key is making the columns dynamic based on the provided interface type. For example, if it's Country, use cId, cName, cFlag
instead of static values.