[英]How to avoid loops duplication?
過濾集合有一個特定的邏輯:
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;
});
}
正如您所注意到的,我嘗試啟動下一個循環以查找 prev 的元素返回空結果。
如何改進?
您可以鏈接過濾器
let rows = (collection || []).filter(callbackFn).filter(callbackFn)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.