Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular filter empty array in an object

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.

like image 767
riyu Avatar asked Mar 15 '26 06:03

riyu


1 Answers

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;
    });
like image 152
Muhammad Abid Avatar answered Mar 17 '26 20:03

Muhammad Abid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!