我正在尝试将响应列表转换为不同类型的对象。当前进行http调用,返回Observable<Object1>,我想将其转换为{id: string, type: string} (more info below)
getData(): Observable<Object1[]> {
return this.http.get<Object1[]>(this.url);
}我以这样的方式引用:
this.getData()
.pipe(
//I understand this can transform each entry into { id: string, type: string } undefined
map(res => res))
.subscribe(res => console.info(res));对象设置:
class Object1 {
name: string;
setId: string;
date: string;
} 任何如何实现这一目标的建议都将不胜感激。
转换将导致Object2类型的对象列表如下:
Object2
{
id: Object1.setId,
type: Object1.name
}JSON反应
{
"name":"Test1",
"setId":"1",
"date":"3456"
},
{
"name":"Test2",
"setId":"2",
"date":"44556"
}发布于 2018-06-09 07:12:03
考虑到它是一个数组,您还需要使用数组的map函数。这将确保正在转换数组中的每个项。
您可以执行以下操作:
this.getData()
.pipe(
map(res => res.map(item => ({id: item.setId, type: item.name})))
).subscribe(res => console.info(res));https://stackoverflow.com/questions/50771680
复制相似问题