How do I most efficiently structure a "conditions" object to find matches within a list of objects (consisting of key value pairs) and add them to a list of "matches?
I have a the following testList that contains multiple objects:
var testList = {
"A": "SUBA1",
"B": "SUBB2",
"C": "SUBC5",
},
{
"A": "SUBA2",
"B": "SUBB3",
"C": "SUBC1",
}
...
]
Right now my matchCondition with one condition is simple focused on one key and value:
var matchCondition = {"key": "A", val:"SUBA1"};
I want to shove any individual object in testList that matches my "matchCondition" into the "matchedList".
var matchedList = [];
So I do this now using "findMatches" function:
function findMatches(matchCondition, testList) {
var matchedList = [];
for(var i = 0; i < testList.length; i++) {
if(testList[matchCondition[key]] == testList[i][matchCondition[val]]) {
matchedList.push(testList[i]);
}
}
return matchedList;
}
My problem is, what if I want to match using multiple conditions something where for example "A" could be equal to "SUBA1" or "SUBA2", AND "B" is "SUBB2", AND "C" is "SUBB5":
Maybe the object could look like this?
var matchCondition = {
"A": ["SUBA1", "SUBA2"],
"B": ["SUBB2"],
"C": ["SUBC5"]
}
I am not sure what is the best way I can update my findMatches function to be more robust... or how do I best structure my "matchCondition" to be able to support multiple key values I want to match?