映射数组嵌套在Angular 2中的对象内

时间:2017-04-11 19:56:28

标签: angular

如何在BankCodes节点下映射数组?

我有以下JSON:

{
  "BankCodes": [
    {
      "BankID": 1,
      "BankDescription": "Banamex",
      "BankName": "Banco Nacional de México",
      "BankStatus": 1
    },
    {
      "BankID": 2,
      "BankDescription": "Bancomext",
      "BankName": "Banco Nacional de Comercio Exterior SNC",
      "BankStatus": 1
    }
  ]
}

以下是我对服务映射的看法:

  getBankInfos(): Observable<BankInfo[]> {
    return this._http.get(this._bankInfoUrl)
        .map((response: Response) => <BankInfo[]> response.json())
        .do(data => console.log('All: ' +  JSON.stringify(data)))
        .catch(this.handleError);
  }

1 个答案:

答案 0 :(得分:0)

只需对调用.json()的结果使用JavaScript的点访问器语法。

  getBankInfos(): Observable<BankInfo[]> {
    return this._http.get(this._bankInfoUrl)
        .map((response: Response) => <BankInfo[]> response.json().BankCodes)
        .do(data => console.log('All: ' +  JSON.stringify(data)))
        .catch(this.handleError);
  }