0

I have a service response like below.

  "amount": 3330,
  "count": 56,
  "dataObjects": [
    {
      "links": {
        "self": "http://localhost:4200/api/collection/"
      },
      "id": "W391",
      "objectNumber": "IN-W391",
      "title": "Image 23",
      "webImage": {
        "guid": "89de22aa-e19f-4c83-87ff-26dd8f319c05",
        "width": 1200,
        "height": 800,
        "url": "http://localhost:4200/api/collection/7qzT0pbclLB7y3fdS1GxzMnV7m3gD3gWnhlquhFaJSn6gNOvMmTUAX3wVlTzhMXIs8kM9IH8AsjHNVTs8em3XQI6uMY=s0"
      }
    },
    {
      "links": {
        "self": "http://localhost:4200/api/collection/"
      },
      "id": "W391",
      "objectNumber": "IN-W391",
      "title": "Image 23",
      "webImage": {
        "guid": "89de22aa-e19f-4c83-87ff-26dd8f319c05",
        "width": 1200,
        "height": 800,
        "url": "http://localhost:4200/api/collection/7qzT0pbclLB7y3fdS1GxzMnV7m3gD3gWnhlquhFaJSn6gNOvMmTUAX3wVlTzhMXIs8kM9IH8AsjHNVTs8em3XQI6uMY=s0"
      }
    }
  ]
}

All i am trying to do is to retrieve just the dataObjects array so that i can loop through it and bind the url under webImage and display it in the UI. here is what i have tried

Component.ts

ngOnInit() {
this.artistService.GetArtistDetails().pipe(map( response=> response.dataObjects))
    .subscribe((artists: any[])=>{
      console.log(artists);
    });  
  }
}

Service.ts

 public GetArtistDetails(){
    return this.httpClient.get(this.REST_API_SERVER);
  }

Thanks in advance

1
  • In your service file, can you cast it directly to a model type? e.g. method GetArtistDetails(): Observable<DataObjects[]> { // then use .pipe(map(result=>result.dataObjects ))} Commented Jun 5, 2021 at 6:29

1 Answer 1

1

In addition to what A2Ia said, here is a sample code that works as desired:

stackblitz app: https://stackblitz.com/edit/angular-ivy-1ixvmx?file=src/app/app.component.ts

result: https://angular-ivy-1ixvmx.stackblitz.io

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

Comments

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.