【发布时间】:2023-03-14 21:11:01
【问题描述】:
我有一个第三方 API,它返回如下数据 - 它是使用 HttpClient 从 Angular 服务调用的。
const someObject = {
employees:[
{name:"XYZ",age:30},
{name:"ABC",age:28},
]
}
现在我有一个interface,其结构如下 -
interface EmployeeData{
campus:{
property1:string;
property2:string;
};
details:{
name:string;
age:number
}
}
所以EmployeeData.details 完全模仿someObject.employees 结构
现在我想从我的服务中返回数据,如下所示 -
getData():Observable<EmployeeData[]>{
}
因此,员工 someObject 中的数组 应该映射到 EmployeeData.details
这应该返回为EmployeeData[].
我怎样才能做到这一点?
我尝试了以下方法,但它给出了不同的结果。
getData():Observable<EmployeeData[]> {
return this.http.get<any>(url).pipe(
tap(value => console.log(value)),
map(data => data.employees),
map(employees =>{
return employees.map(employee =>{
return{
details:{
name:employee.name,
age:employee.age,
}
}
}
}
)
}
它返回的是 -
details:{}
details:{}
但我想要的是:
{
details:{}
}
{
details:{}
}
有人可以帮忙吗?
【问题讨论】: