Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何高效地编写以下代码?_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 如何高效地编写以下代码?

Javascript 如何高效地编写以下代码?,javascript,angular,typescript,Javascript,Angular,Typescript,我正在应用基于单选按钮选择的过滤器,如何在不使用if条件的情况下使其更有效 applyExtraFilter() { this.moreFilter = !this.moreFilter; this.allItems = this.listings; if (this.deliveryConcession[0].checked) { this.allItems = this.allItems.filter(fil =>

我正在应用基于单选按钮选择的过滤器,如何在不使用if条件的情况下使其更有效

 applyExtraFilter() {
        this.moreFilter = !this.moreFilter;
        this.allItems = this.listings;
        if (this.deliveryConcession[0].checked) {
            this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.deliveryConcession[0].checked);
        }
        if (this.deliveryConcession[1].checked) {
            this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.deliveryConcession[2].checked);
        }
        if (this.deliveryConcession[2].checked) {
            this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.deliveryConcession[3].checked);
        }
        if (this.seatConcession[0].checked) {
            this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.seatConcession[0].checked);
        }
        if (this.seatConcession[1].checked) {
            this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.seatConcession[1].checked);
        }
        if (this.seatConcession[2].checked) {
            this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.seatConcession[3].checked);
        }
        this.setPage(1);
    }
您可以使用
forEach()
函数

注意:如果您迭代
this.delivery让步
this.seatConcession
变量的所有元素,会更容易。在你的例子中,我必须在每个关于索引的循环中添加额外的条件

applyExtraFilter(){
this.moreFilter=!this.moreFilter;
this.allItems=this.listings;
this.deliveryventive.forEach((v,i)=>v.checked&(i==0 | | i==2 | | i==3)?this.allItems=this.allItems.fil=>fil.seatingventive.parking==v.checked):v);
this.seatConcession.forEach((v,i)=>v.checked&(i==0 | | i==1 | | i==3)?this.allItems=this.allItems.fil过滤器(fil=>fil.seatingprevention.parking==v.checked):v);
本文件第1页;

}
这只会过滤一次,您肯定可以做更多的改进

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
来声明
结果
,如果该关键字使您的IDE出现问题,请改用
var

您显式访问
this.deliverydeference
项而不是对其进行迭代的具体原因是什么?实际上没有具体原因我为您提供了一个解决方案,但我意识到,在第一种情况下,您只对
[0,2,3]进行迭代
在第二种情况下,
[0,1,3]
。尝试只过滤一次,最后可以应用所有过滤器。您可以使用类似于
let filters={delivery1:true,delivery2:false,delivery3:true,seat1:true…}
的结构来保存当前ifs的值。这是延迟复制和粘贴还是筛选器不与if语句匹配?如果过滤器要匹配,那么这非常简单