在Angular中使用lodash对对象数组进行筛选

5

我正在使用Angular 7,希望对从后端接收到的对象列表应用多个过滤器。 这些对象是我称之为"CV"类型的。 "CV"中包含其他对象,如下所述。

CV {
  employee: Employee;
  certifications: Certification[];
}
Employee {
  empId: number;
  empAgency: string;
  empBu: string;
}

我的主要问题是,我需要根据嵌套对象的属性来过滤简历列表。

这篇教程启发了我,让我学会了如何应用多个过滤器:https://angularfirebase.com/lessons/multi-property-data-filtering-with-firebase-and-angular-4/

我在 TypeScript 中尝试了以下代码..

  employees: CV[];
  filteredEmployees: CV[];
  //array I use to filter
  filters = {};
  
  //properties I wanna filter by
  empAgency: string;
  empBu: string;
  //standard entry to delete a filter property value
  private DefaultChoice: string = "DefaultChoice";
  
   private applyFilters() {
    if (this.filteredEmployees.length === this.employees.length) {
      this.filteredEmployees = _.filter(this.filteredEmployees,     _.conforms(this.filters));
    } else {
      this.filteredEmployees = this.employees;
      this.filteredEmployees = _.filter(this.filteredEmployees, _.conforms(this.filters));
    }
  }
filterExact(property: string, property2: string, rule: any) {
    if (rule ==='DefaultChoice') {
      delete this.filters[property][property2];
      this.filters[property] = [];
      this.filters[property][property2] = null;
    }
    else {
      this.filters[property] = [];
      this.filters[property][property2] = val => val === rule;
    }
    this.applyFilters();
  }

但是它不能处理嵌套属性。我的问题是我不知道如何正确传递嵌套对象的属性到我的函数中。

有人可以帮忙吗?

通过帮助,我也包括任何关于如何在Angular中链式多个过滤器(使用/不使用lodash)的建议。我仍然是这个领域的新手,所以请随意指向好的方向!


你应该在 https://stackblitz.com 上提供一个 [mcve],以便人们可以帮助你解决问题。阅读你的问题让我意识到它很难处理,有很多无用的信息或业务信息,这些信息对我们回答你的问题没有帮助。 - user4676340
为什么要使用下划线而不是 TypeScript 的默认数组 filter() 函数? - Stavm
@trichetriche 谢谢您的建议。我也是 Stackoverflow 的新手,所以我不知道我需要在 stackblitz 上发布,我会尽快去做。然而,我不会说我的例子非常冗长,实际的代码要复杂得多,对象也更加复杂。我想清楚地表达我的思维过程,并展示我有一个实际工作的代码,在某个点上它不再工作了。 - marcoB
@Stavm 因为根据我的理解,使用 filter() 链接过滤器会意味着一定的顺序:你先应用第一个,然后映射结果,再应用第二个,以此类推。在我的情况下,我想要一个完全可逆的过滤器堆栈,可以随时应用。我想从任何一个过滤器开始,并且如果回到默认值,就取消过滤器属性。但是,如果您认为使用 filter() 更容易,请指向一个解决方案? - marcoB
1个回答

0
根据您提供的示例,我认为您不需要将两个属性都传递给filterExact()。相反,您只需要编写其他过滤函数来执行您想要完成的第二级过滤即可:
  filterExactAgency(property: string, rule: any) {
    this.filters[property] = val => val == rule
    this.applyFilters()
  }

  filterExactBusinessUnit(property: string, rule: any) {
    this.filters[property] = val => val == rule
    this.applyFilters()
  }


感谢您抽出时间回答。是的,我可能想要将过滤器拆分为多个过滤器,但我的问题是我不完全理解如何使用我使用的方法正确地指示过滤器查找嵌套属性。 - marcoB
就目前而言,“applyFilters()”将已经应用来自各种过滤器函数的属性的多个过滤器。如果您只是好奇如何做到这一点,我认为它看起来像是“applyFilters()”和“filterExact()”的组合。 - Ben Hulan
不,如果你按照我的代码片段现在的样子来使用,它们是无法工作的。只要filterExact()接受一个直接属性(例如empAgency)作为参数,它就能正常工作。但现在我需要传递类似employee.empAgency这样的内容,但它却无法正常工作,我不明白其中的原因。 - marcoB
因为当你说 filters[property] 时,你会向 filters 对象添加 { property: property }。但是如果你说filters[property][property2],你将会向对象添加{ property: property: { property2: property2 } },这不是你想要的。 - Ben Hulan
如果你输入 filters[property, property2] 会发生什么? - Ben Hulan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接