There is a specific logic for filtering collection:
let rows = (collection || []).filter((item: any) => {
const { country, year, productType, units, productSubtype } = item;
const cond1 = unitsPlain.length ? unitsPlain.includes(units) : true;
const cond3 = years.includes(year);
const cond2 = country === value;
const cond4 = productType === key && !productSubtype;
return cond1 && cond2 && cond3 && cond4;
});
if (!rows.length) {
rows = collection.filter((item: any) => {
const { country, year, productType, productSubtype } = item;
const cond3 = years.includes(year);
const cond2 = country === value;
const cond4 = productSubtype === key && !productType;
return cond2 && cond3 && cond4;
});
}
if (!rows.length) {
rows = collection.filter((item: any) => {
const { country, year, productType, productSubtype } = item;
const cond3 = years.includes(year);
const cond2 = country === value;
const cond4 = productSubtype === key || productType === key;
return cond2 && cond3 && cond4;
});
}
As you noticed I try to launch the next loop to find elements of prev returns empty result.
How to improve it?
You can chain filters
let rows = (collection || []).filter(callbackFn).filter(callbackFn)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.