Javascript filter with array of conditions with es6
本问题已经有最佳答案,请猛点这里访问。
我正在尝试根据条件数组进行筛选
1 2 | let myArr = [{id:1, performed:1},{id:12, performed:1},{id:34, performed:13}] let conditon_id = [ 12,34,34] //these values are dynamic |
通过过滤
1 | myArr.filter(item => item.performed === 1 && //now I am stuck ) |
我一直在添加检查程序,因为该项目的ID为12或34。值12和34不断变化。我怎么做这个
我也检查了这个问题和这个解决方案,但都没有解决条件数组和额外条件的问题。
我如何做到这一点?
只需添加
1 | myArr.filter(item => item.performed ===1 && condition_id.includes(item.id)); |
据我所知,您需要使用条件数组作为ID筛选myarr。
然后你就可以写了
1 | myArr.filter(item=>item.performed === 1 && conditon_id.indexOf(item.id) !== -1) |
这就是工作
1 2 3 4 5 6 7 8 9 10 | let myArr = [{"id":1,"performed":1},{"id":12,"performed":1},{"id":34,"performed":13}] let conditon_id = [ 12,34,34]; let filtered=myArr.filter(value => { if(value.performed==1 && conditon_id.includes(value.id)) { return value; } }) console.log(filtered) |