I have a data set of recipes that I am trying to filter based on an array of inputs.
const [cuisineInput, setCuisineInput] = useState("");
const [cuisineFilter, setCusineFilter] = useState<Filter[]>([]);
const [filteredResults, setFilteredResults] = useState<any>(null);
const filterDataByCuisine = useCallback(() => {
if (cuisineFilter !== null) {
const filteredData = data!.hits.filter(({ recipe }: any) => {
return Object.values(recipe.cuisineType)
.join("")
.includes(Object.values(
cuisineFilter.map((el) => {return el.label;}))
.join("")
.toLowerCase()
);
});
setFilteredResults(filteredData);
}
}, [cuisineFilter, data]);
Here I am setting the filtered data (filteredData) to a new state (filteredResults). I am then mapping over the new data of filtered data to display in the UI. The Issue I am having is I can not filter by multiple input "lables" in the cuisineFilter array. I am mapping over the cuisineFilter array to grab the label because it is an object containing {id: number, label: string} that I am also displaying in the UI. recipe.cuisineType is an array of a single string like ["italian"].
If I only have one "label" in the cuisineFilter array it works fine and filteres the data as expected, but I want to be able to filter by multiple "labels". How can I make the data filter by multiple inputs in the cuisineFilter array?