Rxjs 无法获取观察值数组
我有一个方法,它应该返回一个数组在一个可观察的。我应该对数组进行一些更改,然后才返回它。现在我只返回一个Rxjs 无法获取观察值数组,rxjs,Rxjs,我有一个方法,它应该返回一个数组在一个可观察的。我应该对数组进行一些更改,然后才返回它。现在我只返回一个AlArticleShort类型的元素。如何返回类型为AlArticleShort[]的数组 Response.shortArticlesInfo是类型为AlArticleShort的对象数组。我必须对该数组执行一些操作并返回修改后的数组 public getArticles():可观察{ 返回此.apiBaseService.get('getBlogArticles').pipe( 以(1)
AlArticleShort
类型的元素。如何返回类型为AlArticleShort[]
的数组
Response.shortArticlesInfo
是类型为AlArticleShort
的对象数组。我必须对该数组执行一些操作并返回修改后的数组
public getArticles():可观察{
返回此.apiBaseService.get('getBlogArticles').pipe(
以(1)为例,
开关映射(响应=>{
返回response.shortArticlesInfo;
}),
地图(文章=>{
this.getAllArticlesTags$().subscribe(标记=>{
控制台日志(标签);
return article.tags=tags.filter(tag=>{
退货物品.tagid.includes(tag.id);
});
});
返回[]
}),
);
}
您可以通过使用map
而不是mergeMap
或switchMap
来避免使用第二个管道此外,如果您只想订阅一次标签,则可以使用
with latestfrom
最终结果应类似于:
public getArticles(): Observable<AlArticleShort[]> {
return this.apiBaseService.get<GetArticlesResponse>('getBlockArticles').pipe(
take(1),
withLatestFrom(this.getAllArticlesTags$()),
map(([{ shortArticlesInfo }, tags]) =>
shortArticlesInfo.map(article => {
return {
...article,
tags: tags.filter(tag => article.tagIds.includes(tag.id))
}
})
)
)
}
public getArticles():可观察{
返回此.apiBaseService.get('getBlockArticles').pipe(
以(1)为例,
使用LatestFrom(this.getAllArticlesTags$()),
map([{shortArticlesInfo},tags])=>
shortArticlesInfo.map(article=>{
返回{
文章
tags:tags.filter(tag=>article.tagIds.includes(tag.id))
}
})
)
)
}