簡體   English   中英

角度過濾器對象中的空數組

[英]angular filter empty array in an object

我有這個對象數組

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);

我想過濾所有那些帶有空數組的正確的correctChoiceIds

var filterNoCorrectChoice= $filter('filter')(list, { correctChoiceIds: [] }, true);

這就是我提出的,但它根本沒有給我什么。 我不確定這是正確的方式還是為什么它沒有結果。

看看演示

像這樣使用過濾器,

  var filterNoCorrectChoice= $filter('filter')(list,function(item) {
        // must have array, and array must be empty
        return item.correctChoiceIds && item.correctChoiceIds.length === 0;
    });

方法過濾原生

var filterQuestion = list.filter(function(el){ return el.correctChoiceIds.length<1; });
// result
console.log(filterQuestion);

我想過濾所有那些帶有空數組的正確的correctChoiceIds

你不需要角度過濾器,使用標准的原生javascript過濾器......

list.filter(x => !x.correctChoiceIds.length);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM