我正在使用一个 API,该 API 可向我返回一些有关照片的数据,但我想在名为 favorite: boolean
的响应中添加一个标志,以便我可以轻松管理照片是否是否是最喜欢的。
来自 API 的普通响应看起来像这样(但更简单):
{
"photos": [
{ "id": 1001, "url": "site.com/img/1001" },
{ "id": 1002, "url": "site.com/img/1002" }
]
}
我想知道是否有办法添加 favorite
标志,以便我使用的对象看起来像这样:
{
"photos": [
{ "id": 1001, "url": "site.com/img/1001", "favorite": false },
{ "id": 1002, "url": "site.com/img/1002", "favorite": false }
]
}
这是我从 API 获取响应的代码:
getRecentPhotos(): Observable<any> {
let photos = this.http
.get(this.getRecentPhotosUrl)
.map((res: Response) => {
return res.json();
})
.catch(this.handleError);
return photos;
}
是否可以将 favorite
标志添加到我从该函数返回的响应中?
谢谢
最佳答案
你已经掌握了大部分代码,你只需要在映射器中添加 favorite
标志即可,
.map((res: Response) => {
let _result = res.json();
_result = _result.map(_x => {
_x = {favorite: false};
return _x;
})
return _result ;
})
关于javascript - 如何轻松使用 API 响应,同时向响应添加一些额外的标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39070719/