0

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>

1 Answer 1

3

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

}

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, it is working now! Thank you! I will mark your answer but do you maybe know why my code did not work as expected?
Because subscription is not awaitable, you have to work with data in subscribe method. If you want use await you can do await this.sp.getReleases().toPromise(); and then use filter method on array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.