使用$filter根据数组中对象的属性进行筛选

Filtering based on properties of objects within array using $filter

本文关键字:属性 筛选 对象 filter 数组 使用      更新时间:2023-09-26

假设以下数据:

var roster = [
    {
        id: 1,
        attended: true,
        person: {printName: 'Larry'}},
    {
        id: 2,
        attended: false,
        person: {printName: 'Curly'}},
    {
        id: 3,
        attended: true,
        person: {printName: 'Moe'}}];

我正在尝试查找数组中参与的对象数是否为真。我尝试过以下几种:

rosters.html:

{{ (roster | filter:{attended:true} ).length }}

花名册控制器.js:

checkedInCount: function() {
    return $filter('filter')($scope.roster, attended.true).length;
}

html过滤器按预期工作,在本例中返回2。但是,函数版本遇到错误ReferenceError: Can't find variable: attended。我认为函数中遗漏了一些琐碎的东西,但我不确定它是什么。

使用对象作为表达式:

return $filter('filter')($scope.roster, { attended: true }).length;