Logik
Logik

Reputation: 219

Why is my array changing after function return?

I have a function, where I filter an array and check whether the property liked of the objects equals one with the help of a checkbox.

  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)
    }

In my html file I iterate through those matches:

        <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>

Upvotes: 0

Views: 58

Answers (1)

user7499628
user7499628

Reputation:

I think that you wrong use rxjs, try this

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 });
});

}

Upvotes: 3

Related Questions