【问题标题】:unable to skip first data value from each row using filter, map or skip operators of Rxjs library in Angular无法使用 Angular 中 Rxjs 库的过滤器、映射或跳过运算符跳过每一行中的第一个数据值
【发布时间】:2026-01-19 11:35:01
【问题描述】:

如何在 Angular 中使用 Rxjs 库的过滤器、映射或跳过运算符在订阅之前跳过服务中的特定数据值(全名)

基本上,我只需要在订阅服务之前使用运算符来获取所需的数据并跳过一些数据值

this.employee-service.my-Service(page-Number-And-Records-Per-Page)
          .pipe(skip(1),)
          .subscribe(
          //https://angular.io/tutorial/toh-pt4
            (res) => {
              console.log(res.data);
              this.employees = res.data;
              this.sorting(); //A-Z is default
          }
          );

仅在订阅前尝试使用 0 个“男性”相关记录过滤性别,但不起作用

【问题讨论】:

  • 如果我理解您的问题,您可以使用filter()

标签: angular filter rxjs pipe


【解决方案1】:

您可以对响应数据使用数组过滤器

this.employee-service.my-Service(page-Number-And-Records-Per-Page)
          .pipe(map(res=>res.data.filter(obj=>obj.gender===0))))

【讨论】:

  • 你能分享一下stackblitz的工作示例吗?
  • 问题已更新,请相应回复
  • this.employee-service.my-Service 返回 res.data。作为数组。对吗?
  • yes Array(20) 0: {id: "1205", fullName: "some value", shortName: "ak", gender: "0", department: "fgiufioguf iuiou", ...} 1:{...}2:{...} ...
  • Rxjs filter() 是过滤出具有一定条件的流发射,与数组过滤器不同。在您的情况下,您的发射返回一个 res 对象,其中 res.data 作为数组,您必须过滤的是这个数组。使用我修改后的答案,您将 map 与数组 filter 一起使用,这应该会在 subscribe() 中为您提供正确的返回
【解决方案2】:

您可以使用过滤器过滤我们的索引或您的值,在这种情况下您需要过滤索引所以只需添加

.pipe(filter((val, index) => index > 0))

或者您也可以过滤响应数据

.pipe(
map(arr=>arr.filter(item=>item.name!=='filterName')))

【讨论】:

  • 你能分享一下stackblitz的工作示例吗?
  • 问题已更新,请相应回复