Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  12] [ 5]  / answers: 1 / hits: 19066  / 4 Years ago, thu, september 24, 2020, 12:00:00

I'm starting with react and I have an array of objects called arrayToFilter I want to apply multiple filters to, ideally when a user changes the filter select options all filters should be applied to the array and the results returned into a filteredResults array to show, I already have each filter function and their constants but I don't know how to apply all the filters one after another...


Here is my code:


    const [ arrayToFilter, setArrayToFilter ] = useState([ /*array of objects to apply filters to*/ ]);
const [ filteredResults, setFilteredResults] = useState([ /*array of filtered objects*/ ]);
const [ categoryOptions, setCategoryOptions ] = useState([ 1,2,3 ]);
const [ selectedCategoryOptions, setSelectedCategoryOptions ] = useState([ ]);
const [ searchName, setSearchName ] = useState('');
const [ hideVenuesWithoutDiscounts, setHideVenuesWithoutDiscounts ] = useState(true);

const filterHideVenuesWithoutDiscounts = () =>
{
if(hideVenuesWithoutDiscounts)
{
return arrayToFilter.filter(item => item.discounts.length > 0);
}
else
{
return arrayToFilter;
}
};

const filterSelectedCategoryOptions = () =>
{
return arrayToFilter.filter(item => item.category_id.includes(selectedCategoryOptions));
};

const filtersearchName = () =>
{
return arrayToFilter.filter(item => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1);
};

useEffect(() =>
{
//Filter options updated so apply all filters here
},
[searchName, hideVenuesWithoutDiscounts, selectedCategoryOptions]);

Notice useEffect, afaik this is like watch property in vue, I'd like to fire all the filters when a filter input changes


More From » reactjs

 Answers
51

It's simple, you can pass the filtered array as a parameter to the filtering functions so that they can filter the filtered array.


For example:


const filterHideVenuesWithoutDiscounts = (array) => {
if (hideVenuesWithoutDiscounts) {
return array.filter((item) => item.discounts.length > 0);
} else {
return array;
}
};

const filterSelectedCategoryOptions = (array) => {
return array.filter((item) => item.category_id.includes(selectedCategoryOptions));
};

const filtersearchName = (array) => {
return array.filter((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1);
};

useEffect(() => {
//Filter options updated so apply all filters here
let result = arrayToFilter;
result = filterHideVenuesWithoutDiscounts(result);
result = filterSelectedCategoryOptions(result);
result = filtersearchName(result);
setArrayToFilter(result);
}, [searchName, hideVenuesWithoutDiscounts, selectedCategoryOptions]);

[#50637] Sunday, September 6, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaya

Total Points: 531
Total Questions: 107
Total Answers: 100

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;