我正在根据单选按钮的选择应用过滤器,如何在不使用if条件的情况下提高效率
{{1}}
答案 0 :(得分:1)
您可以使用forEach()
功能。
注意:如果要迭代this.deliveryConcession
和this.seatConcession
变量的所有元素,会更容易。在你的情况下,我必须在每个关于索引的循环中添加额外的条件。
applyExtraFilter() {
this.moreFilter = !this.moreFilter;
this.allItems = this.listings;
this.deliveryConcession.forEach((v, i) => v.checked && (i == 0 || i == 2 || i == 3) ? this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == v.checked) : v);
this.seatConcession.forEach((v, i) => v.checked && (i == 0 || i == 1 || i == 3) ? this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == v.checked) : v);
this.setPage(1);
}

答案 1 :(得分:0)
这只会过滤一次,你可以肯定做更多的改进。
applyExtraFilter() {
this.moreFilter = !this.moreFilter;
this.allItems = this.listings;
this.allItems = this.allItems.filter(fil => {
let result = true;
if (this.deliveryConcession[0].checked && fil.seatingConcession.parking != this.deliveryConcession[0].checked) {
result = false;
}
if (this.deliveryConcession[1].checked && fil.seatingConcession.parking != this.deliveryConcession[2].checked) {
result = false;
}
if (this.deliveryConcession[2].checked && fil.seatingConcession.parking != this.deliveryConcession[3].checked) {
result = false;
}
if (this.seatConcession[0].checked && fil.seatingConcession.parking != this.seatConcession[0].checked) {
result = false;
}
if (this.seatConcession[1].checked && fil.seatingConcession.parking != this.seatConcession[1].checked) {
result = false;
}
if (this.seatConcession[2].checked && fil.seatingConcession.parking != this.seatConcession[3].checked) {
result = false;
}
return result;
});
this.setPage(1);
}
我已使用let
声明result
,如果该关键字为您提供了使用var
的IDE问题。