Javascript多条件数组过滤器 [英] Javascript multiple condition array filter

查看:107
本文介绍了Javascript多条件数组过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need help putting together an array search that is based on multiple conditions. Furthermore, all the conditions are conditional, meaning I may or may not need to filter on those conditions. What I have:

要过滤的对象数组:

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
}]

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

I am filtering the above array of objects based on another array of mixed elements. The elements represent the following search fields:


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

  • "searchstring": to search on all fields in the data array for any matched text sequence

对象,其键值为reprsenting帐户类型,true或false
为值,表示是否应该用于过滤

object with key values reprsenting account type and a true or false for value indicating if it should be used to filter

startdate过滤转换

startdate to filter transdate on

enddate过滤转换

enddate to filter transdate

要过滤类别的类别名称

拥有搜索条件看起来像这样(但是如果某些字段不是必需的,它们将被设置为undefined或只是一个空字符串或数组):

The array that has the search conditions looks like this (but if some of the fields are not necessary they will be set to undefined or just an empty string or array):

var filtercondition = {
    "p",
    {acct1:true,acct2:false,acct3:true...}
    "2016-06-01",
    "2016-11-30",
    "category3"
}

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

What is the best way to accomplish this? What I've devised is a separate search for each element in the filter array, but this seems non optimal and very tedious. I'm open to a redesign of my setup...

推荐答案

// 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;
});

两个音符:
- 更改过滤条件的大括号以便它是一个数组,但我建议使用一个对象。
- 这个 {acct1:true,acct2:false,acct3:true ...} 样本对我来说没有意义,因为它表明 acct 字段应同时 acct1 acct3

Two notes: - changed the braces of filtercondition so that it is an array, however I would suggest to use an object instead. - this {acct1:true,acct2:false,acct3:true...} sample doesn't make sense for me, since it suggests that the acct field should be acct1 and acct3 at the same time.

这篇关于Javascript多条件数组过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆