angular - 使用 RxJS 将一种对象类型转换为另一种对象类型
问题描述
我有以下型号:
export interface IdName {
id: number;
name: string;
}
export interface Product {
id: number;
name: string;
currency?: string;
additionalData: any;
}
我想在 Angular/Typescript 中将 Observable<Product[]> 转换为 Observable<IdName[]>。我试过了
getData() : Observable<IdName[]> {
// Note: getProducts returns Observable<Product[]>
this.productService.getProducts().pipe(
return map((product: Product) => {
{id: product.id, name: product.name}
});
);
}
但是,它显示了一些语法错误。我该如何解决?
解决方案
您放错了return
语句和分号。
它应该是:
return this.productService.getProducts().pipe(
map((product: Product) => {
return {id: product.id, name: product.name}
})
);
pipe()
接受定义为operator(input => output)
或扩展的操作列表(可变参数)operator(input => { return output; })
推荐阅读
- java - 下载、解压缩并将 zip 文件解析为 Java 对象 - 无法从 ZipInputStream 读取字节
- flutter - 如何返回未来
结果是飞镖 - php - HTTP 错误 403 在我的测试页面中被禁止,nginx 服务器
- python - 寻找最短的唯一子串
- telegraf - 通过 REST api 调用时,mavg 波前查询超时
- spring - Spring:UnsatisfiedDependencyException 即使限定符属性匹配
- mysql - MySQL 服务器远程连接
- python - 如何更改输入字符串中的单个字母
- sql - 呼叫者的第一个和最后一个呼叫是同一个人
- r - 为什么我在计算平均值时会得到 NA?