angularjs过滤具有对象值的对象数组

时间:2015-11-12 16:24:28

标签: angularjs javascript-framework

如果我有这样的简单对象:

$scope.results = {
  year:2021,
  subjects:[
    {title:'English',grade:'A'},
    {title:'Maths',grade:'A'},
    {title:'Science',grade:'B'},
    {title:'Geography',grade:'C'}
  ]
};

使用过滤器获取价值并不是问题,例如

 $scope.gradeC = $filter('filter')($scope.results.subjects, {grade: 'C'})[0];

或算一下:

 $scope.gradeA = $filter('filter')($scope.results.subjects, {grade: 'A'}).length;

但是,如果我有这样的事情:

$scope.results = {
  year:2021,
  subjects:[
    {title:'English',grade:''},
    {title:'Maths',grade:''},
    {title:'Science',grade:''},
    {title:'Geography',grade:'{
                 _someDate : 'Nov 19, 2024'
             }'}
  ]
};

我怎么能用空字符串计算等级中的多少值以及将某些对象作为值多少?

1 个答案:

答案 0 :(得分:2)

过滤器在视图中很有用。但是在JS代码中,你拥有标准JS函数的所有功能:

var countWithEmptyGrade = $scope.results.subjects.filter(function(subject) {
    return subject.grade === '';
}).length;