我在下面的课。我想做的是过滤活动程序和非活动程序,然后获取这些程序的程序名称(以字符串数组的形式)。
这是程序类:
export class Program {
ID: number;
ProgramDescription: string;
ProgramName: string;
ProgramStatus: string;
FiscalYear: string;
SupervisorID: string;
DBN: string;
IsActive: string;
}
以下是我在服务中编写的方法:
getPrograms(): Observable<Program[]> {
return this.httpClient.get<Program[]>(environment.apiBaseURL + 'api/Program')
.pipe(
map(data => data)
);
}
getActivePrograms(): Observable<Program[]> {
return this.getPrograms()
.pipe(
map(data => data.filter(program => program.IsActive === 'Y'))
);
}
getActiveProgramNames(): Observable<string[]> {
return this.getActivePrograms()
.pipe(
map(data => data.filter(program => program.ProgramName.toString())
);
}
在getActiveProgramNames()中,出现以下错误:
Type Program[] is not assignable to type string[]
答案 0 :(得分:1)
尝试使用map
getActiveProgramNames(): Observable<string[]> {
return this.getActivePrograms()
.pipe(
map(data => data.map(program => program.ProgramName.toString())
);
}