gpt4 book ai didi

angular - 在 Angular 上使用 lodash 过滤对象数组

转载 作者:太空狗 更新时间:2023-10-29 17:40:01 25 4
gpt4 key购买 nike

我在 Angular 7 上工作,我想对从后端收到的对象列表应用几个过滤器。这些对象属于我称之为“CV”的类型。CV 内部是其他对象,如下所述。

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

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

我从本教程中获得了很多灵感来应用多个过滤器: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)的任何其他建议。我在这个领域还是个新手,所以请随时为我指出好的方向!

最佳答案

根据您链接的示例,我认为您不需要将这两个属性都传递给 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()
}

关于angular - 在 Angular 上使用 lodash 过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com