【问题标题】:How to Map array Of Json Object Into another Plain json Object in TypeScript Angular 2如何在 TypeScript Angular 2 中将 Json 对象数组映射到另一个普通 json 对象
【发布时间】:2018-07-20 02:49:00
【问题描述】:

响应正文;

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

通过使用来自 DB 的 Rest 调用获取 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 中提出建议

【问题讨论】:

    标签: json rest typescript angular2-services


    【解决方案1】:

    也许你可以使用解构,

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

    您也可以从以下位置导入“地图”,

    import 'rxjs/add/operator/map';
    

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 1970-01-01
      • 2020-03-08
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多