首页 文章

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

提问于
浏览
0

响应机构;

[
  {
    "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表中的数据Kindly Suggest in angular 2 Typescript

1 回答

  • 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';
    

相关问题