0

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()??

3
  • 1
    You are getting files as an array, couldn't you just use it in your function? Also on what basis you need to filter the files? Commented Apr 3, 2018 at 13:58
  • I don't see detailresponse Commented Apr 3, 2018 at 13:58
  • @Explosion Pills 2 detailresponse is higher in hierarchy in this JSON Commented Apr 3, 2018 at 16:32

2 Answers 2

0

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);
          });
      });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works fine. Thanks!
0

Assume your service returns a JSON object array. Within your subscription function, you need to iterate over it as a general array and then parse it.

this.api.getAttachments()
   .subscribe( data => {
            data.files.forEach(file => { 
                          console.log(file.title, file.link, file.itemId)

An easy way to identify the structure is to place a debugger in the subscribe call, and see the object hierarchy by expanding it in the dev console. Then you can base your parsing on that hierarchy.

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.