[Fixed] Filtering empty json response in Typescript

Issue

Question – I am trying to filter the image property of this json response.
Some of the arrays dont contain images and therefore I want to filter them out.
New to angular and typescript.

this.music.searchArtists(this.searchQuery).subscribe(
  data =>{
    this.results = data.artists.items;
    console.log(this.results);
    
  }
)

Response
data.artists.items

2 contains images, 3 does not (therfore want to exclude it)
second image

Solution

Try this.

this.results = data.artists.items.filter(item => item.images.length > 0);

Leave a Reply

(*) Required, Your email will not be published