Angular2 RxJs 映射 http 响应
Angular2 RxJs map http response
我有 api 其中 returns 照片对象列表,我想使用 subscribe() 对其进行迭代.. 不是一次全部,而是一张一张的..
http.get('photos.json')
.map(res => res.json());
我可以使用哪些函数(地图、平面图...)将响应转换为多个数组,因此在使用订阅时,它会一个一个地迭代..而不是一次所有响应。
json example file
您可以使用 flatMap
:
http.get('photos.json')
.map(res => res.json())
.flatMap((array, index) => array)
.filter(photo => 600 <= photo.height)
.subscribe(photo => console.log(photo))
Flatmap 会这样做,如果你从结果数组中创建一个 Observable。
yourPhotosArray.flatMap(photos => Observable.from(photos))
会将您的照片数组转换为 Observable
我有 api 其中 returns 照片对象列表,我想使用 subscribe() 对其进行迭代.. 不是一次全部,而是一张一张的..
http.get('photos.json')
.map(res => res.json());
我可以使用哪些函数(地图、平面图...)将响应转换为多个数组,因此在使用订阅时,它会一个一个地迭代..而不是一次所有响应。
json example file
您可以使用 flatMap
:
http.get('photos.json')
.map(res => res.json())
.flatMap((array, index) => array)
.filter(photo => 600 <= photo.height)
.subscribe(photo => console.log(photo))
Flatmap 会这样做,如果你从结果数组中创建一个 Observable。
yourPhotosArray.flatMap(photos => Observable.from(photos))
会将您的照片数组转换为 Observable