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

问题描述 投票:0回答:1

我有一个函数,在这个函数中,我过滤了一个数组,并检查是否有属性 liked 的对象等于1,并通过复选框的帮助。

  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>
javascript arrays angular typescript async-await
1个回答
3
投票

我认为你用错了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 });
});

}

© www.soinside.com 2019 - 2024. All rights reserved.