Value of typeOfFilters is selected from different component, so in my current component I get value of currency and frequency, but my filter function doesn't iterate over frequency.
I want to add more filters to my filter function. Do tell me how can i simplify this problem? plans looks like this.
let plans = [
{
"amount": 6000,
"currency": "USD",
"frequency": "MONTH",
"planId": "plan_L7mrzVML9o6G5c",
"planName": "computer",
"provider": "stripe"
},
{
"amount": 2000,
"currency": "INR",
"frequency": "DAY",
"planId": "plan_KkHkJPEVR7ZA2s",
"planName": "Platinum",
"provider": "stripe"
}
]
const typeOfFilters = {
currency: ['INR'],
frequency: ['DAY'],
provider: "",
amount: "",
status: "",
}
const sortedTableArray = plans.filter(function (plan) {
for (const [f, values] of Object.entries(typeOfFilters)) {
if (!typeOfFilters[f]) {
return true;
}
if (
f === "currency" &&
typeOfFilters["frequency"].indexOf(plan[f]) > -1
) {
return true;
}
if (f === "currency" && typeOfFilters[f].indexOf(plan[f]) > -1) {
return true;
}
if (plan[f] === undefined || plan[f] !== typeOfFilters[f]) {
return false;
}
}
});
```
expected output should return the table entry which has the frequency inside typeOfFilter['frequency']
plans,typeOfFiltersand the expected outputfilterisn't checking for all the filter keys. If the first key is falsy,if (!typeOfFilters[f]) return trueadds the object in the returned array