Angular6 使用RxJS映射运算符返回对象数组

Angular6 使用RxJS映射运算符返回对象数组,angular6,rxjs6,Angular6,Rxjs6,我有一个第三方API,它返回如下数据-它是使用HttpClient从Angular Service调用的 const someObject = { employees:[ {name:"XYZ",age:30}, {name:"ABC",age:28}, ] } 现在我有一个接口,结构如下- interface EmployeeData{ campus:{ property1:string; propert

我有一个第三方API,它返回如下数据-它是使用
HttpClient
从Angular Service调用的

const someObject = {

    employees:[   

        {name:"XYZ",age:30},
        {name:"ABC",age:28},
    ]
}
现在我有一个
接口
,结构如下-

interface EmployeeData{

   campus:{

     property1:string;
     property2:string;

   };
   details:{

     name:string;
     age:number

   }
}
getData():Observable<EmployeeData[]>{

}
所以
EmployeeData.details
完全模仿
someObject.employees
结构

现在我想从我的服务返回数据,如下所示-

interface EmployeeData{

   campus:{

     property1:string;
     property2:string;

   };
   details:{

     name:string;
     age:number

   }
}
getData():Observable<EmployeeData[]>{

}
它返回的是-

details:{}
details:{}
但我想要的是:

{
    details:{}
}
{
    details:{}
}

有人能帮忙吗

如果要使用
Observable
键入函数,则返回的Observable必须包含实现
EmployeeData
接口的对象数组,这意味着您需要完全定义它们,而不仅仅是
details
属性

如果此时无法定义所有属性,则必须使用带有
作为
关键字的类型断言。请注意,由于缺少的属性将是未定义的,因此您将丢失类型安全性并可能会遇到错误

getData(): Observable<EmployeeData[]> {
  return this.http.get<any>(url)
    .pipe(
      map(data => data.employees),
      map(employees => employees.map(employee => ({ details: employee } as EmployeeData)))
    );
}
getData():可观察{

返回此.http.get

如果要使用
Observable
键入函数,则返回的Observable必须包含实现
EmployeeData
接口的对象数组,这意味着您需要完全定义它们,而不仅仅是
details
属性

如果此时无法定义所有属性,则必须使用带有
作为
关键字的类型断言。请注意,您将失去类型安全性,并且可能会遇到错误,因为缺少的属性将无法定义

getData(): Observable<EmployeeData[]> {
  return this.http.get<any>(url)
    .pipe(
      map(data => data.employees),
      map(employees => employees.map(employee => ({ details: employee } as EmployeeData)))
    );
}
getData():可观察{

返回此.http.get

我已更新我的答案,它工作正常,请参阅附件中的stackblitz链接。我已更新我的答案,它工作正常,请参阅附件中的stackblitz链接。