简体   繁体   English

如何在TypeScript Angular 2中将Json对象数组映射到另一个Plain json对象

[英]How to Map array Of Json Object Into another Plain json Object in TypeScript Angular 2

Response Body; 反应机构;

[
  {
    "professionalId": {
      "cib_code": "30003",
      "thirdPartyId": "RS30004"
    },
    "nationalIdentifier": "984538926",
    "nationalIdType": "SIREN"
  },
  {
    "professionalId": {
      "cib_code": "30003",
      "thirdPartyId": "RS30008"
    },
    "nationalIdentifier": "944316926",
    "nationalIdType": "SIREN"
  }
]

By Using Rest Calling from DB getting json Response: 通过使用DB的Rest调用获取json响应:

this.thirdpartyServices.getThirdparties().subscribe(data=>this.thirdpartyapis$=data);

Getting Given Above Json Array Response in **thirdpartyapis** object **thirdpartyapis**对象中的Json数组响应之上获取信息

but i wanted another object which can contain this data in simple format like 但是我想要另一个可以包含简单格式的数据的对象,例如

[
  {
    "cib_code": "30003",
    "thirdPartyId": "RS30004"
    "nationalIdentifier": "984538926",
    "nationalIdType": "SIREN"
  },
  {
    "cib_code": "30003",
    "thirdPartyId": "RS30008"
    "nationalIdentifier": "944316926",
    "nationalIdType": "SIREN"
  }
]

want to map my data in Typscript so that that same object can be use in Html to Display data in Grid table Kindly Suggest in angular 2 Typescript 想要在Typscript中映射我的数据,以便可以在HTML中使用同一对象在Grid表中显示数据请在angular 2 Typescript中建议

Maybe you could use destructuring, 也许您可以使用解构,

this.thirdpartyServices.getThirdparties()
  .map(each => {
    let { nationalIdentifier, nationalIdType } = each;
    return {
      ...each.professionalId,
      nationalIdentifier,
      nationalIdType
    };
  })
  .subscribe(data => {
    this.thirdpartyapis$ = data;
  });

You may also import 'map' from, 您也可以从以下位置导入“地图”:

import 'rxjs/add/operator/map';

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

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