I have this array of objects
var list = [{
"questionId": 1,
"correctChoiceIds": [],
"choiceId": 0,
"number": 1
}, {
"questionId": 1,
"correctChoiceIds": [1234, 4567],
"choiceId": 0,
"number": 2,
}, {
"questionId": 2,
"correctChoiceIds": [],
"choiceId": 0,
"number": 3
}];
//This filter gives me an array with list[0] and list[1]
var filterQuestion = $filter('filter')(list, { questionId: 1 }, true);
I want to filter all those with an empty array for their correctChoiceIds.
var filterNoCorrectChoice= $filter('filter')(list, { correctChoiceIds: [] }, true);
This is what I came up with but it simply gives me nothing. I'm not sure if this is the right way or why it results to nothing.
see demo
Use the filter like this,
var filterNoCorrectChoice= $filter('filter')(list,function(item) {
// must have array, and array must be empty
return item.correctChoiceIds && item.correctChoiceIds.length === 0;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With