2015-11-12 88 views
0

如果我有簡單的對象是這樣的:angularjs器陣列中值

$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' 
      }'} 
    ] 
}; 

我該如何計算空字符串的等級中有多少值,以及一些對象的值是多少?

回答

2

過濾器在視圖中很有用。但在JS代碼中,您擁有標準JS功能的所有功能:

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

謝謝,很好的解決方案! – Arthur