javascript - 针对嵌套属性过滤/匹配数组 - 多个过滤器
问题描述
我有一个简单的选择。选择返回一个字符串值,例如“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
任何帮助表示赞赏。
解决方案
尽管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);
推荐阅读
- database - 如何使用打字稿/角度将点击次数/禁用/启用按钮存储在数据库中?
- ios - BSImagePicker 所有图像模糊
- facebook-graph-api - 升级 Facebook Graph API 版本
- python - Autodesk Maya API 自定义节点
- c++ - 如何解决此错误“'x1' 的值在常量表达式中不可用”?静态断言
- kotlin - 强制转换和安全转换运算符与 null 有什么区别
- three.js - 带点着色器的threejs metaballs
- r - 我想使用 R 中的简单命令绘制数据帧的 NA
- r - 过滤具有至少一个特定值的行
- textures - 在 WebGL2 中渲染为 16 位无符号整数 2D 纹理