为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的数组在函数返回后会发生变化?

为什么我的数组在函数返回后会发生变化?

慕沐林林 2022-09-11 20:12:48
我有一个函数,在其中我过滤一个数组,并在复选框的帮助下检查对象的属性是否等于1。liked  async getGetLikedMatches(checked){    if(checked){      await this.sp.getReleases().subscribe(data => {        this.allMatches = data;        this.matches = data;      });      this.matches = this.allMatches.filter(match => match.favorit == 1);      console.log({matches: this.matches, allMatches: this.allMatches}); // matches has correctly 6                                                                          // elements      // after the fuction returns matches == allMatches -> 110 element      // but why?    }    else if (!checked){      await this.sp.getReleases().subscribe(data => {        this.allMatches = data;        this.matches = data;      });      console.log(this.matches)    }在我的html文件中,我迭代这些匹配项:        <div class="col-3" *ngFor="let match of matches">            <img [src]="match.images == '' ? url : getImage(match)"              alt="matches"              style="min-width: 200px; max-width: 200px; max-height: 200px; min-height: 200px;"             (click)="onDetailView(match.id, selectedType)"/> <br />        </div>
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

我认为你错误地使用了rxjs,试试这个


getGetLikedMatches(checked) {

    this.sp.getReleases().subscribe(data => {

       this.allMatches = data;


       if (checked) {

           this.matches = this.allMatches.filter(match => match.favorit == 1);

       } else {

           this.matches = data;

       }


       console.log({ matches: this.matches, allMatches: this.allMatches });

});

}


查看完整回答
反对 回复 2022-09-11
  • 1 回答
  • 0 关注
  • 83 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信