Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  87] [ 7]  / answers: 1 / hits: 66034  / 7 Years ago, fri, january 13, 2017, 12:00:00

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:



Array of objects to filter:



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: to search on all fields in the data array for any
    matched text sequence


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


  • startdate to filter transdate on


  • enddate to filter transdate


  • category name to filter category on




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...


More From » filtering

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


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.


[#59371] Wednesday, January 11, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oliverg

Total Points: 453
Total Questions: 101
Total Answers: 100

Location: Liechtenstein
Member since Wed, Dec 8, 2021
2 Years ago
;