I have an array of objects with the following structure
arr = [ { name: "abc" , items: ["itemA","itemB","itemC"], days :138} ,
{ name: "def" , items: ["itemA1","itemB2","itemC1"], days :157} ,
{ name: "hfg" , items: ["itemAN","itemB7","itemC7"], days :189} ]
This array needs to be filtered based on the search input passed. I was able to achieve the same for the name
, where days is not getting filtered.
Also can someone help how to search across items
array too so it filters the rows based on input passed
This is what I have tried
handleSearch = (arr, searchInput) => {
let filteredData= arr.filter(value => {
return (
value.name.toLowerCase().includes(searchInput.toLowerCase()) ||
value.days.toString().includes(searchInput.toString())
);
});
console.log(filteredData);
//this.setState({ list: filteredData });
}
value.days.toString().includes(searchInput.toLowerCase())
?value.items
days
includesdays
, which is nonsense. Anyway, just use[item.name, item.items.join(','), item.days].join(',')
to get a comma separated string containing all the strings and the days, then useincludes()
on that