首页 > 解决方案 > 针对嵌套属性过滤/匹配数组 - 多个过滤器

问题描述

我有一个简单的选择。选择返回一个字符串值,例如“foo”。然后,我将字符串值与列表中的所有条目进行匹配。只有匹配的条目才会返回。entry.eventType.description === this.ngxValue; // "foo" === "foo" returns true

例如单选

// this.ngxValue = "foo"

private multiEventTypeFilter(entry) {
    if (this.ngxValue === 'Any') {
        return true;
    } else {
        return entry.eventType.description === this.ngxValue;
    }
}

多选

我现在更改了我的选择以允许多选。现在返回一个由所有选定值组成的数组,例如["foo", "bar", "car"]. 所以现在我想返回与该数组中的任何值匹配的所有条目。this.ngxValue不再是单个字符串,而是包含多个字符串的数组。以下是想要的...

entry.eventType.description === this.ngxValue; // "foo" === ["foo", "bar", "car"] returns true

任何帮助表示赞赏。

标签: javascriptarraysangulartypescriptfiltering

解决方案


尽管Array.prototype.includes()是满足要求的直接方式,但还有另一种替代方法Array.prototype.some()。由于您正在寻找其他替代方案,请在此处提及,以便以后随时提供帮助

下面是如何使用的例子Array.prototype.some()

let data = ["foo", "bar", "car"]

console.log(data.some(d => d === "foo"))

根据您的要求,以下是方式

return this.ngxValue.some(val => val === entry.eventType.description);

推荐阅读