让我们以下面的英雄代码为例来获取英雄:
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
);
}我想操作hero[]的每个元素。我尝试过.map函数:
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
).map((result) => {
console.log(result); // I am getting all the array and not iteration.
return Object.assign(new Hero(), result);
);
}但我得到的是所有的数组或Hero[],而不是一个接一个地迭代英雄。
发布于 2018-03-28 02:47:26
Map操作的是您当前获得的Observable数据,而不是其中的每个数据。因为您将英雄放入数组中,而不是一个接一个,所以可以在map中将整个数组作为单个数据。
您需要对结果使用Array.map,创建映射数组并将其传递给下一个数组。
RxJS 5现在使用do而不是tap。
https://stackoverflow.com/questions/49520282
复制相似问题