【发布时间】:2017-05-28 08:02:28
【问题描述】:
我需要帮助来整理基于多个条件的数组搜索。此外,所有条件都是有条件的,这意味着我可能需要也可能不需要过滤这些条件。我有什么:
要过滤的对象数组:
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
}]
我正在根据另一个混合元素数组过滤上述对象数组。这些元素代表以下搜索字段:
“searchstring”:在数据数组中的所有字段中搜索任何 匹配的文本序列
具有代表帐户类型和真假的键值的对象 用于指示是否应用于过滤的值
过滤transdate的开始日期
过滤transdate的结束日期
要过滤类别的类别名称
具有搜索条件的数组看起来像这样(但如果某些字段不是必需的,它们将被设置为未定义或只是一个空字符串或数组):
var filtercondition = {
"p",
{acct1:true,acct2:false,acct3:true...}
"2016-06-01",
"2016-11-30",
"category3"
}
实现这一目标的最佳方法是什么?我设计的是对过滤器数组中的每个元素进行单独搜索,但这似乎不是最优的并且非常乏味。我愿意重新设计我的设置...
【问题讨论】:
-
数据的预期大小是多少?预计将在哪里运行?最近的电脑?移动的?旧电脑?强大的服务器? “最佳方式”实际上取决于多种因素。
标签: javascript filtering