获取仅点击ID的JSON数据

时间:2019-02-26 07:58:29

标签: angular typescript

我有一组JSON数据。它的基础有2个ID,每个ID内还有其他附件数据。我试图单击一个基本ID,并将其附件数据显示在第二页上。我如何只获取有关单击ID的信息?

{
    "_embedded": {
        "deliveryMessageList": [
            {
                "id": "73c624c9-6db7-4fd2-ac91-c1084aee0565",
                "attachments": [
                    {
                        "id": "fb1e6d31-4c4e-4356-be5a-827ea5ec422d",
                        "attachToDeliveryMessage": false
                    },
                    {
                        "id": "160edceb-cda7-483c-a2b9-786f583b523d",
                        "attachToDeliveryMessage": true
                    }
                ]
            },
            {
                "id": "bfd600ad-754f-444d-bddb-f5cf4d7727b8",
                "attachments": [
                    {
                        "id": "e4454f8c-4ecb-444e-a82a-318bbcee1c11",
                        "attachToDeliveryMessage": false
                    },
                    {
                        "id": "791a73eb-59cc-4bba-8b16-d442169a7923",
                        "attachToDeliveryMessage": true
                    }
                ]
            }
        ]
    }
}

我的用户界面看起来像这样

example

我已经尝试像这样向点击事件添加ID,但不确定下一步该怎么做

  getCustomerAccountDocs(selectedItem: any, index: number) {
    this.CustomerAccountDocsService.getCustomerAccountDocs()
    .subscribe((data: any) => {
      this.customerAccountDocs = data;
    });
  }

HTML代码

  <tr *ngFor="let item of customerAccountDocs; let i=index" (click)="getCustomerAccountDocs(item, i)">
    <td> {{item.accountNumber}}</td>  
    <td> {{item.id}}</td>
  </tr>

1 个答案:

答案 0 :(得分:1)

听起来似乎很像一个问题,即如何获取特定JSON节点的所有其他兄弟姐妹。我不知道您是如何(通过循环或其他方式)完全访问ID的,但是我仅向您展示如何循环访问ID。假设上面引用的主要JSON对象存储在名为“ main”的变量中,然后从那里开始工作:

var embedded = main["_embedded"]["deliveryMessageList"] //or however u access the JSON keys..
var newJSON = {};
embedded.forEach(x => { 
    for(var k in x) {
        var currentIDsiblings = {};
        var ID = null;
        for(var subkey in x[k]) {
            if(subkey != "id") {

                currentIDsiblings[subkey] = (x[k][subkey]);
            } else {
                ID = subkey;
            }

        }
        newJSON[ID] = currentIDsiblings;
    }
});

/* now you can access newJSON[myID], to get all of its siblings */