Javascript多条件数组过滤器

mo_*_*aat 5 javascript filtering

我需要帮助整理基于多个条件的数组搜索.此外,所有条件都是有条件的,这意味着我可能需要也可能不需要对这些条件进行过滤.是)我有的:

要过滤的对象数组:

var data = [{
    "_id" : ObjectId("583f6e6d14c8042dd7c979e6"),
    "transid" : 1,
    "acct" : "acct1",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category1",
    "amount" : 103
},
{
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 2,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category2",
    "amount" : 103
},
{
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 3,
    "acct" : "acct2",
    "transdate" : ISODate("2016-07-31T05:00:00.000Z"),
    "category" : "category1",
    "amount" : 103
},
{
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 4,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category2",
    "amount" : 103
},
{
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 5,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category3",
    "amount" : 103
},
{
    "_id" : ObjectId("583f6e6d14c8042dd7c152g2"),
    "transid" : 6,
    "acct" : "acct3",
    "transdate" : ISODate("2016-10-31T05:00:00.000Z"),
    "category" : "category3",
    "amount" : 103
}]
Run Code Online (Sandbox Code Playgroud)

我正在根据另一个混合元素数组过滤上面的对象数组.元素代表以下搜索字段:

  • "searchstring":在数据数组中的所有字段上搜索任何匹配的文本序列

  • 具有键值的对象表示帐户类型,对于表示是否应该用于过滤的值,则为true或false

  • startdate过滤转换

  • enddate过滤transdate

  • 要筛选类别的类别名称

具有搜索条件的数组如下所示(但如果某些字段不是必需的,则它们将设置为undefined或仅设置为空字符串或数组):

var filtercondition = {
    "p",
    {acct1:true,acct2:false,acct3:true...}
    "2016-06-01",
    "2016-11-30",
    "category3"
}
Run Code Online (Sandbox Code Playgroud)

完成此任务的最佳方法是什么?我设计的是对滤波器数组中的每个元素进行单独搜索,但这似乎不是最优的并且非常繁琐.我愿意重新设计我的设置......

And*_*erd 9

创建一个函数数组,每个函数代表一个条件。

这是一些演示该方法的示例代码......

 var conditions = [];

 // Dynamically build the list of conditions
 if(startDateFilter) {
    conditions.push(function(item) { 
       return item.transdate >= startDateFilter.startDate;
    });
 };

 if(categoryFilter) {
     conditions.push(function(item) {
         return item.cateogry === categoryFilter.category;
     });
 };
 // etc etc
Run Code Online (Sandbox Code Playgroud)

一旦有了条件数组,您就可以使用Array.prototype.every在项目上运行每个条件。

 var itemsMatchingCondition = data.filter(function(d) {
     return conditions.every(function(c) {
         return c(d);
     });
 });
Run Code Online (Sandbox Code Playgroud)

或者,使用更紧凑的箭头函数:

const itemsMatchingCondition = data.filter(d => conditions.every(c => c(d));
Run Code Online (Sandbox Code Playgroud)


ale*_*zyk 5

// You wrote that it's an array, so changed the braces 
var filtercondition = ["p",
{acct1:true,acct2:false,acct3:true...}
"2016-06-01",
"2016-11-30",
"category3"
];

var filtered = data.filter(o => {
    if(filtercondition[0] && !o.category.includes(filtercondition[o])) { // checking just the category, but you can check if any of more fields contains the conditions 
        return false;
    }
    if(filtercondition[1]) {
        for(var key in filtercondition[1]) {
        if(filtercondition[1][key] === true && o.acct != key) {
            return false;
        }
        }
    }
    if(filtercondition[2] && o.transdate < filtercondition[2]) {
        return false;
    }
    if(filtercondition[3] && o.transdate > filtercondition[3]) {
        return false;
    }
    if(filtercondition[4] && o.category !== filtercondition[4]) {
        return false;
    }

    return true;
});
Run Code Online (Sandbox Code Playgroud)

两个注意事项:-更改了花括号filtercondition,使其成为数组,但是我建议改用对象。-这个{acct1:true,acct2:false,acct3:true...}样本没有意义对我来说,因为它表明该acct字段应该acct1acct3在同一时间。