I am trying to filter an data array of objects with an two dimensional filter array of objects.
My Data array looks something like this:
dataArray = [{
Filter_GroupSize: "1"
Filter_Time: "5-30"
title: "Tools Test 1"
}, {
Filter_GroupSize: "5-10"
Filter_Time: "60-120"
title: "Tools test 2"
}, {
Filter_GroupSize: "5-10"
Filter_Time: "120-240"
title: "Tools 3"
}, {
Filter_GroupSize: "10-20"
Filter_Time: "240-360"
title: "Tools Test 4"
}]
The thing is that the two dimensional filter array can change over time. In my example down here it has the length of 2. But over time it can get bigger or smaller. If the 2D filter array changes so does the dataArray.
Heres how my two dimensional filter array of objects looks like now.
FiltersToApply = [
[{
choice: "5-10"
internalColName: "Filter_GroupSize"
}, {
choice: "10-20"
internalColName: "Filter_GroupSize"
}],
[{
choice: "60-120"
internalColName: "Filter_Time"
}, {
choice: "120-240"
internalColName: "Filter_Time"
}]
]
I want to filter through the data array and only return element that has both the values of the "Filter_GroupSize" and "Filter_Time" from the 2D Filter Array. In this example i would want to have the "Tools test 2" and "Tools 3" element returned in a new array. I want a filter function that checks the "FiltersToApply[0]" and if it finds a match it checks the "FiltersToApply[1]" for match on the same element from the dataArray.
I have come this far with the code:
for (let i = 0; i < FiltersToApply.length; i++) {
console.log(FiltersToApply[i]);
FiltersToApply[i].forEach((filter) => {
var filteredArray: Tool[] = dataArray.filter((item) => {
if (item[filter.internalColName] == filter.choice && ((i+1) <= FiltersToApply.length)) {
this.nextArray(FiltersToApply[i+1], item);
}
});
});
}
But i am stuck here. Anyone have a good solution?