简体   繁体   中英

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. Below is the Sample of the JSON I am receiving.

{  
  "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"

I need to filter this data out and then form a new JSON Array with this data alone. 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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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