I have some JSON data that I am trying to filter based on the checkbox selection of a user in javascript but I have run into a wall as to the multi-level filtering. I have two dimensions of data that need to be filtered from the JSON; first filter the JSON data based on the OS selection, then to filter that resulting OS selection data with a questions selected filter. For example, I am doing the following:
$('.os-checkbox input[type="checkbox"]:checked').each(function() {
OSSelected.push(Number($(this).parents('.checkbox').index()));
});
$('.question-checkbox input[type="checkbox"]:checked').each(function() {
QuestionSelected.push(Number($(this).parents('.checkbox').index()));
});
var array = $scope.data.responses;
var osSelectionFilter = array.filter(function(elem, index) {
return (JSON.stringify(elem.OSSelectedIndex) == JSON.stringify(OSSelected));
});
console.log(osSelectionFilter); // should return all 'data.response.OSSelectedIndex' objects with the selection made, ex. 0-PC so any selection with "OSSelectedIndex": [0] included
var questionSelectionFilter = osSelectionFilter.filter(function(elem, index) {
return (JSON.stringify(elem.questionSelectedIndex) == JSON.stringify(QuestionSelected));
});
console.log(questionSelectionFilter); // should filter the osSelectionFilter above and return all objects with the questions array included, ex. 0, 1, 2...
Once a selection is filtered the "result" data in the JSON will populate a div. Issue is now that the filter is trying to filter the whole array I am guessing, e.g. looking for [0, 1] instead of each value in the array individually; where value [0] and [1] are two different separate selections.
JSON data coming from the CMS looks like this:
{
"id": "test1",
"fields": {
"OS": [
"PC",
"Mac",
"Android",
"iOS"
],
"questions": [{
"question": "Question 0"
}, {
"question": "Question 1"
}, {
"question": "Question 2"
}, {
"question": "Question 3"
}, {
"question": "Question 4"
}, {
"question": "Question 5"
}, {
"question": "Question 6"
}],
"responses": [{
"ID": 1,
"OSSelectedIndex": [ 0 ],
"questionSelectedIndex": [],
"result": "<h1>Response 1 found</h1>"
}, {
"ID": 2,
"OSSelectedIndex": [ 0, 1 ],
"questionSelectedIndex": [ 0, 1, 2 ],
"result": "<h1>Response 2 found</h1>"
}, {
"ID": 3,
"OSSelectedIndex": [ 0, 2 ],
"questionSelectedIndex": [ 0, 1, 2, 5 ],
"result": "<h1>Response 3 found</h1>"
}, {
"ID": 4,
"OSSelectedIndex": [ 1, 2 ],
"questionSelectedIndex": [ 1, 2, 3, 4 ],
"result": "<h1>Response 4 found</h1>"
}]
}
}
Is such a setup possible?
Plunker with the code above:
https://plnkr.co/edit/N1NJKDOgvVkB9gPmPooi?p=preview
Thanks so much