首页 > 解决方案 > 过滤两个带有条件的数组

问题描述

我有两个共享公共列的数组。我想在数组上应用过滤器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'));

标签: angulartypescript

解决方案


您可以尝试以下方法:

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 ]

推荐阅读