首页 > 解决方案 > 在 Angular rxjs 中转换 API 响应

问题描述

嗨,我收到了来自 API 的回复:

{
   "records":[
      {
         "id":1,
         "motivazione":"",
         "autorizzazione":false,

      }
   ]
}

我如何像这样转换它:

[
      {
         "id":1,
         "motivazione":"",
         "autorizzazione":false,

      },
   ]

谢谢

约瑟夫

标签: javascriptangularapirxjs

解决方案


You can use the map operator in rxjs to transform your data. More precisely if you only want the records property from your response object you can do that as following: 

import { map } from 'rxjs/operators';

transformData(){
     this.http.get(url).pipe(map((target: any) => target.records)).subscribe((data) => {
   console.log(data);
    })}

这将仅从您的响应对象返回记录属性。


推荐阅读