angular - RxJS operator with array
问题描述
I think I have a misunderstanding on how works RxJS with an array when I receive it from an HTTP call.
For example I have:
public getNews(): Observable<News[]> {
return this.http.get<News[]>("/news");
}
And after I want to use a simple map on it:
this.newsService.getNews().pipe(map(news => {
return {
// DO SOMETHING HERE WITH NEWS
};
}));
The problem is the type of my param, Typescript told me its a array of News but it's inside a map so normally it has to of type News right?
I don't know If I'm clear, but if someone can explain me it would be great :)
解决方案
Hi to convert an array to a list of items you could use the flatMap
operator; the you can work on each item in the subscribe
or trasform with map
this.service.getNews().pipe(
flatMap(x => x)
).subscribe(x => {
// do something for each item
});
or
this.service.getNews().pipe(
flatMap(x => x),
map(x => {
// transform each item
})
);
the second is still an Observable
推荐阅读
- google-apps-script - 如何授予 Apps Script Web App 授权以使用服务帐户为域中的所有用户访问 Google 表格
- javascript - 如何在反应中将数据从无状态子组件发送到无状态父组件?
- python-3.x - 将任意数量的参数从列表传递给函数?
- php - 如何包含来自古腾堡插件子文件夹的脚本
- android - 可变自定义视图和 Android Studio 设计/布局编辑器
- android - 如何在添加成功侦听器中传递查询
- api - 如何在 API Manager 中激活新实例并在 mule 上停止旧实例?
- flutter - 如何使用 CustomPainter 绘制矩形画布视图
- javascript - DriveApp createFile 添加未定义到文件
- ios - 在上传应用商店期间分发内容按钮