Angular 如何基于模式使用RxJS可观测值过滤数据
我在Angular 2应用程序中使用RxJS observables。我试图获取员工列表,如果员工id以“MA”结尾,我不希望他们添加到列表中。以下是我的代码:Angular 如何基于模式使用RxJS可观测值过滤数据,angular,typescript,rxjs5,Angular,Typescript,Rxjs5,我在Angular 2应用程序中使用RxJS observables。我试图获取员工列表,如果员工id以“MA”结尾,我不希望他们添加到列表中。以下是我的代码: getEmployees(): any { return this.employeeService.get("http://localhost:9080/employees") .map((employees: any) => employees.map((employee: any
getEmployees(): any {
return this.employeeService.get("http://localhost:9080/employees")
.map((employees: any) => employees.map((employee: any) => {
let empcode: string = employee.empcode;
if (empcode.lastIndexOf("MA") == -1) {
return { empText: empcode+ ' - ' + employee.empName, data: employee};
}
return { empText: '', data : null};
}));
}
我无法筛选记录,它将返回所有值。我有两个return语句,因此它将返回所有值,但如果我删除其中一个,则会出现以下错误:
返回表达式中不存在最佳通用类型
web服务返回的json格式如下:
{
employeeCode: "EMPCT",
employeeName: "Tom",
role: "HR"
}
因此,您能告诉我在这种情况下过滤web服务返回的记录的最佳方法是什么吗。为什么不使用
过滤功能
getEmployees(): any {
return this.employeeService.get("http://localhost:9080/employees")
.map((employees: any) => items.filter((employee: any) => {
let empcode: string = employee.empcode;
return ((ifscode.lastIndexOf("MA") == -1);
}));
}
不知道ifscode来自哪里。无论如何,你应该使用过滤器。如果需要进一步转换,可以链接.map()
操作。为什么不使用过滤器
功能
getEmployees(): any {
return this.employeeService.get("http://localhost:9080/employees")
.map((employees: any) => items.filter((employee: any) => {
let empcode: string = employee.empcode;
return ((ifscode.lastIndexOf("MA") == -1);
}));
}
getEmployees(): any {
return this.employeeService.get("http://localhost:9080/employees")
.map((employees: any) => {
let filtered = employees.json().filter((employee) => {
let empcode: string = employee.empcode;
if (ifscode.lastIndexOf("MA") == -1) {
return true;
} else {
return false;
}
});
return filtered;
}));
}
不知道ifscode来自哪里。无论如何,你应该使用过滤器。如果需要进一步转换,可以链接一个.map()
操作。我想返回empcode和员工姓名,而不是true和false,如果员工代码以MA结尾,我就不想返回该员工。您能告诉我如何使用您建议的解决方案来实现这一点吗。通过它,您将获得API返回的员工的所有详细信息。检查一下,如果可能的话,用API的回复更新你的问题。我已经更新了API返回的json示例。它以这种格式返回记录。在这个问题中,我提到了我希望返回数据的格式,那么我如何使用上述解决方案来实现这一点。您是否通过在调用getEmployees
函数的组件中执行console.log
来检查结果?我在chrome的网络选项卡中检查了结果,它仍然返回代码以MA结尾的员工。我想返回empcode和员工姓名,而不是true和false,如果员工代码以MA结尾,我就不想返回该员工。您能告诉我如何使用您建议的解决方案来实现这一点吗?有了它,您将获得从中返回的员工的所有详细信息应用程序编程接口。检查一下,如果可能的话,用API的回复更新你的问题。我已经更新了API返回的json示例。它以这种格式返回记录。在这个问题中,我提到了我希望返回数据的格式,那么我如何使用上述解决方案来实现这一点。您是否通过在调用getEmployees
函数的组件中执行console.log
来检查结果?我在chrome的网络选项卡中检查了结果,它仍然返回代码以MA结尾的员工。“为什么不使用过滤功能?”是的,他当然应该。但不是数组中的过滤器
。他应该使用Observable
中的过滤器。一般来说,我同意你的观点,这里的序列是由数组组成的。RX过滤器将通过或阻止整个结果,而这不是本例中所需的行为case@Meir我能够得到过滤过的记录。但是我想将employeename+Employeecode返回到组件。你知道我该怎么做吗?在过滤器之后进行原始映射(不进行条件测试):items.filter(…).map(…)“为什么不使用过滤器函数?”是的,他当然应该这样做。但不是数组中的过滤器
。他应该使用Observable
中的过滤器。一般来说,我同意你的观点,这里的序列是由数组组成的。RX过滤器将通过或阻止整个结果,而这不是本例中所需的行为case@Meir我能够得到过滤过的记录。但是我想将employeename+Employeecode返回到组件。你知道我该怎么做吗?在筛选器之后执行原始映射(无条件测试):items.filter(…).map(…)
getEmployees(): any {
return this.employeeService.get("http://localhost:9080/employees")
.map((employees: any) => {
let filtered = employees.json().filter((employee) => {
let empcode: string = employee.empcode;
if (ifscode.lastIndexOf("MA") == -1) {
return true;
} else {
return false;
}
});
return filtered;
}));
}