简体   繁体   English

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

[英]Angular 2 : Convert a complex JSON Object to a Simple one

I am quite new to Angular 2 and I am stuck with a problem where I have to filter out all the unnecessary stuff from the JSON object I am getting from a REST API. 我对Angular 2还是很陌生,我遇到了一个问题,即必须从REST API中获取的JSON对象中过滤掉所有不必要的内容。 Below is the Sample of the JSON I am receiving. 以下是我收到的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"
     }

All I need from this data is just these 2 fields : "rdk":2, "refDataNM":"Afghanistan" 我需要的只是这两个字段:“ rdk”:2,“ refDataNM”:“阿富汗”

I need to filter this data out and then form a new JSON Array with this data alone. 我需要过滤掉这些数据,然后仅使用这些数据形成一个新的JSON数组。 Something like this : 像这样的东西:

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

you could try something like this: 您可以尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM