如何使用基于模式的 RxJS 可观察对象过滤数据

How to filter data using RxJS observables based on a pattern

我在 Angular 中使用 RxJS observables 2 application.I 我正在尝试获取员工列表,如果员工 ID 以 'MA' 结尾,我不想添加他们到列表中。以下是我的代码:

    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 对我所有 values.I 有两个 return 语句所以它 return 对所有值但是如果我删除其中一个,我会收到以下错误:

return 个表达式中不存在最佳通用类型

Web 服务生成的 json return 格式如下:

{
employeeCode: "EMPCT", 
employeeName: "Tom", 
role: "HR"
}

那么,在这种情况下,您能否告诉我过滤由 Web 服务编辑的记录的最佳方法是什么?

为什么不使用 filter 功能?

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) => {
                    let filtered = employees.json().filter((employee) => {
                      let empcode: string = employee.empcode;
                      if (ifscode.lastIndexOf("MA") == -1) {
                        return true;
                      } else {
                        return false;
                      }
                    });
                  return filtered;
                }));
}