angular - 过滤两个带有条件的数组
问题描述
我有两个共享公共列的数组。我想在数组上应用过滤器filters
并获取n_fnc
第二个数组中的所有统计数据conditions
等于动作的位置。
如果它有不同的统计数据,即使一个是“行动”,也不应该被选中
filters [{n_fnc: 2343, name: "jack"} ,{n_fnc:2500 , name:"daniel"},{n_fnc:3000 , name:"trump"}]
Conditions [{id :1 ,n_fnc: 2343, stat:"do"},
{id:2, n_fnc: 2343, stat:"act"},
{id:3, n_fnc: 2343, stat:"plan"},
{id:10, n_fnc: 2500, stat:"act"},
{id:18, n_fnc: 2500, stat:"act"},
{id:20, n_fnc: 3000, stat:"act"}
]
result = [2500,3000]
我尝试使用 every 和 filter 函数,但它没有返回任何内容。
let result = this.filters.filter(o =>
this.conditions.filter(({n_fnc}) => n_fnc === o.n_fnc).every(({stat}) => stat=== 'act'));
解决方案
您可以尝试以下方法:
const r = filters.filter(o => {
const i = Conditions.findIndex(c => c.n_fnc === o.n_fnc);
if(i > -1) {
if(Conditions[i].stat === 'act') {
return true;
}
}
return false;
})
.map(o => o.n_fnc)
console.log(r); // [ 2500, 3000 ]
推荐阅读
- c# - 如何在 C# 中解码 html 编码的字符串
- linux-kernel - Linux 中 x86 架构的 IPI 类型
- sql - Oracle SQL——从 RN 函数中选择最大值,但使用该值更新所有字段
- python - 分配 Sklearn train_test_split() 返回的变量
- python - 尝试从 API 访问项目会出错
- python - 请求模块在 Python 中增加了 api 调用
- java - 无法使用 JavaCPP 与共享对象(.so 文件)交互
- python - 替换 Dataframe 每一行中的特定中文单词
- java - PostgreSQL 11 spring JPA 查询错误,java.sql.Date 对象和 TO_DATE 函数格式为 YYYY-DD-MM
- amazon-s3 - Flink s3 写入性能优化