Search code examples
jsonangularrxjsobservableformattable

How to observe objects in response JSON with no specyfic names in RXJS Angular 5


Situation looks like this. I need to fill mat-table dynamically after receiving a response from API. In response JSON there are those anonymous json objects in json array "files".

"files": [
      {
        "title": "dummy",
        "link": "https://t1.mogree.com/backmeister_content/pdfs/presentation/31347009705473837693867181531764_a.pdf",
        "attachment": "dummy-atachment_name.pdf",
        "userTypes": [
          "ADM",
          "INT"
        ],
        "itemid": "31347009705473837693867181531764_a.pdf",
        "type": 17,
        "itemproviderid": 0,
        "detailtype": 0
      },
      {
        "title": "dummy",
        "link": "https://t1.mogree.com/backmeister_content/pdfs/presentation/46728595498675807527841664269653_a.pdf",
        "attachment": "dummy-atachment_name.pdf",
        "userTypes": [
          "ADM",
          "INT"
        ],
        "itemid": "46728595498675807527841664269653_a.pdf",
        "type": 17,
        "itemproviderid": 0,
        "detailtype": 0
      }

I dont know how to set observable on this imtems of this table. I just getting "files" item. I need to have access to those anonymous objects. For now my function with request looks like this:

ngOnInit() {
    this.apiService.getAttachments()
      .map(data => data['detailresponse'])
      .map(data => data['files'])
      .subscribe(files => {
          });
  }

I need access to fields: "title", "link", "itemId". Maybe i need second observable inside subscribe()??


Solution

  • Try this, and let me know if it works!

    ngOnInit() {
        this.apiService.getAttachments()
          .map(data => data['detailresponse'])
          .map(data => data['files'])
          .subscribe(files => {
              files.forEach(file => {
                  console.log(file.title, file.link, file. itemId);
              });
          });
    }