Currently, I am facing a challenge where I need to filter a data array of objects using a two-dimensional filter array of objects.
Here is a glimpse of my data array:
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 complexity arises from the fact that the two-dimensional filter array can vary in size over time. In this illustration, it consists of 2 arrays, but it can expand or contract. As the filter array changes, so does the data array.
Below is the current structure of my two-dimensional filter array of objects:
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"
}]
]
My goal is to filter the data array and only retrieve elements that match both the "Filter_GroupSize" and "Filter_Time" values from the 2D filter array. For example, given the current filter array, I would like to collect the "Tools test 2" and "Tools 3" elements into a new array. The filter function should scan "FiltersToApply[0]" for a match, then check "FiltersToApply[1]" for a consistent match on the same dataArray element.
I have made progress with the following code snippet:
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);
}
});
});
}
However, I am currently at a roadblock. Any suggestions or solutions would be greatly appreciated!