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

时间:2018-02-09 10:41:36

标签: json rest typescript angular2-services

响应机构;

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

通过使用来自DB的休息调用获取json响应:

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

**thirdpartyapis**对象

中获取Json数组响应

但我想要另一个可以包含这些数据的对象,如

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

想要在Typscript中映射我的数据,以便在Html中使用相同的对象来显示Grid表格中的数据 请使用angular 2 Typescript

建议

1 个答案:

答案 0 :(得分:0)

也许你可以使用解构,

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

您也可以从

导入'map'
import 'rxjs/add/operator/map';