首页 > 解决方案 > 带多重检查的 lodash 动态过滤器

问题描述

我有一个多检查。我需要一个带有一个选项、两个选项或三个选项的过滤器显示。

这是选项:

在此处输入图像描述

该组件有一个数组。数组是动态的:

0: "Active"
​
1: "Inactive"
​
2: "Terminated"

然后对于过滤器,我lodash在里面使用

for(var i = 0; i < dataFilter.length; i++) 
          {
              console.log(dataFilter[i]);
              const results = _.filter(res, function(item) {
                 return (item.project === dataFilter[i]);
              });
              this.items = results;
          }

这是示例res

{
    "id": "1",
    "createdOn": "2020-05-01T23:10:13.000Z",
    "createdBy": 'Juan',
    "Title": "testing",
    "project": "Active",
  }

示例dataFilter

Array [ "Active", "Inactive" ]

但总是更换过滤器,过滤器只过滤一个选项,但有时需要显示过滤器 2 或 3 个选项。

Lodash在这种情况下有另一种过滤方式或者也许有另一个想法?

标签: javascriptlodash

解决方案


遍历 res 数组中的每个项目。获取项目名称。然后检查该项目名称是否存在于 dataFilter 中。如果没有,则删除该项目 - 像这样:

this.items = res.filter(({ project }) => dataFilter.includes(project))

ES6 为数组提供了内置.filter函数,因此无需使用 lodash。


推荐阅读