Angular 2:将复杂的JSON对象转换为Simple对象

时间:2017-09-19 21:54:49

标签: javascript arrays json angular typescript

我对Angular 2很新,我遇到了一个问题,我必须从REST API中获取JSON对象中的所有不必要的东西。以下是我收到的JSON示例。

{  
  "refDataId":{  
     "rdk":1,
     "refDataTypeCD":"CNTRY",
     "refDataStatusCD":"C",
     "effStartDT":"2017-09-01",
     "effEndDT":null,
     "updtUserID":"EDMO",
     "updtTS":"2017-09-05"
  },
  "refDataDescs":[  
     {  
        "rdk":1,
        "langCD":"EN_CA",
        "refDataNM":"Not Applicable",
        "refDataShortNM":null,
        "refDataDesc":"Not issued by ISO. Dummy country code for internal reference use only.",
        "updtUserID":"EDMO",
        "updtTS":"2017-09-05"
     }
  ],
  "refCntry":{  
     "cntryRdk":1,
     "cntryIso2DigitCD":"0",
     "cntryIso3DigitCD":null,
     "cntryIsoNumericCD":0,
     "riskTypeRdk":0
  }

}

{  
  "refDataId":{  
     "rdk":2,
     "refDataTypeCD":"CNTRY",
     "refDataStatusCD":"C",
     "effStartDT":"2017-09-01",
     "effEndDT":null,
     "updtUserID":"EDMO",
     "updtTS":"2017-09-05"
  },
  "refDataDescs":[  
     {  
        "rdk":2,
        "langCD":"EN_CA",
        "refDataNM":"Afghanistan",
        "refDataShortNM":null,
        "refDataDesc":null,
        "updtUserID":"EDMO",
        "updtTS":"2017-09-05"
     }

我从这些数据中得到的只是这两个字段:             " RDK":2,             " refDataNM":"阿富汗"

我需要过滤掉这些数据,然后单独使用这些数据形成一个新的JSON数组。像这样:

{"id":2,"itemName":"Afghanistan"},
{"id":3,"itemName":"Albania"}

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情:

filterDate(input:any[]) {
    const output=[];
    input.foreach( item => output.push({"id": item.refDataId.id, "itemName": item.refDataDescs.refDataNM}));
    return output;
 }