From an array of objects, I am trying to filter based on the response_id
key. This key is nested within the response object. If I input 23764 and 23765, I want to retrieve Question objects that have AT LEAST 2 RESPONSES with those specific ids as the only ones in the array.
If I search with just 23764, I want to get Question objects with a response object containing that id as the only one in the array.
The current implementation I have looks something like this:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$filter) {
$scope.data = [
{
"id": "1341",
"question": "What about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Every thing "
},
{
"response_id": "23765",
"comment": "No"
},
{
"response_id": "23766",
"comment": ".."
}
]
},
{
"id": "1342",
"question": "What dislike about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing"
},
{
"response_id": "23765",
"comment": "No"
},
{
"response_id": "23766",
"comment": ".."
}
]
},
{
"id": "1343",
"question": "What suggestion(s) this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing "
},
{
"response_id": "23765",
"comment": "No"
},
{
"response_id": "23766",
"comment": ".."
}
]
}
];
var res = ($filter('filter')($scope.data, {response:{response_id:'23764,23765'}}, true));
console.log(res);
});
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Desired Result
[
{
"id": "1341",
"question": "What about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Every thing "
},
{
"response_id": "23765",
"comment": "No"
}
]
},
{
"id": "1342",
"question": "What dislike about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing"
},
{
"response_id": "23765",
"comment": "No"
}
]
},
{
"id": "1343",
"question": "What suggestion(s) this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing "
},
{
"response_id": "23765",
"comment": "No"
}
]
}
]