I have a table with datasource(data) with columns 1-8 but i want to search a value only in columns 1,2,3,4 and return the filteredData. The following code doesn’t work and i cant seem to find why.
What i want to do is: when the user enters a keyword, it should search the table data source( array of objects) but only within 4 columns and return the filtered data.
const filterUserSearch = (data, searchState) => {
const searchIndex = ["col1", "col2", "col3", "col4"];
let filteredData = null;
if (searchState) {
const lowercasedFilter = searchState?.toLowerCase();
filteredData = data.filter((item) => {
return searchIndex.forEach((index) => {
item[index].toLowerCase().includes(lowercasedFilter);
});
});
console.log("filteredData", filteredData);
} else {
return data;
}
return filteredData;
};