2016-04-29 118 views
0

我有這個數組:如何過濾對象數組?

$scope.damageEvants = 
    [ 
     {"id":2, "StartDate":21/05/2012, EndDate:null}, 
     {"id":3, "StartDate":null, EndDate:02/09/2014}, 
     {"id":4, "StartDate":null, EndDate:null}, 
     {"id":5, "StartDate":21/05/2012, EndDate:null} 
    ]; 

我要過濾(刪除)從數組,其中財產的startDate和結束日期爲空的所有對象。

過濾後的結果:

$scope.damageEvants = 
    [ 
     {"id":2, "StartDate":21/05/2012, EndDate:null}, 
     {"id":3, "StartDate":null, EndDate:02/09/2014}, 
     {"id":5, "StartDate":21/05/2012, EndDate:null} 
    ]; 

我嘗試這樣做:

$scope.damageEvants.filter(return item.StartDate!== null && item.EndDate !== null ) 

但它似乎是走錯了路。

如何過濾$scope.damageEvants使用過濾功能?

回答

3
$scope.damageEvants.filter(return item.StartDate!== null && item.EndDate !== null) 

應該是

$scope.damageEvants.filter(function(item) { 
    return item.StartDate!== null && item.EndDate !== null; 
}); 

filter

1
$scope.damageEvants = $scope.damageEvants.filter(function(item) { 
    return item.StartDate!== null && item.EndDate !== null; 
}); 
1

只是檢查如果任一日期設置。

var array = [{ "id": 2, "StartDate": '21/05/2012', EndDate: null }, { "id": 3, "StartDate": null, EndDate: '02/09/2014' }, { "id": 4, "StartDate": null, EndDate: null }, { "id": 5, "StartDate": '21/05/2012', EndDate: null }], 
 
    result = array.filter(function (a) { 
 
     return a.StartDate || a.EndDate; 
 
    }); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

ES6版本

var array = [{ "id": 2, "StartDate": '21/05/2012', EndDate: null }, { "id": 3, "StartDate": null, EndDate: '02/09/2014' }, { "id": 4, "StartDate": null, EndDate: null }, { "id": 5, "StartDate": '21/05/2012', EndDate: null }], 
 
    result = array.filter(a => a.StartDate || a.EndDate); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

1

由於舒尚特的回答表明,.filter()預計將收到一個函數值,與該函數返回決定一個布爾值如果當前的數組成員將會進入過濾的數組或不。你的代碼傳遞過濾布爾值而不是函數值。

1

另一種選擇是在angular.forEach循環中拼接對象。這實際上將從原始數組中刪除對象,而不是創建新的數組。

例子:https://jsfiddle.net/gh03zur4/

控制器

 function Controller($scope) { 
     $scope.damageEvants = [{ 
     "id": 2, 
     "StartDate": 21/05/2012, 
     EndDate: null 
     }, { 
     "id": 3, 
     "StartDate": null, 
     EndDate: 02/09/2014 
     }, { 
     "id": 4, 
     "StartDate": null, 
     EndDate: null 
     }, { 
     "id": 5, 
     "StartDate": 21/05/2012, 
     EndDate: null 
     }]; 

     angular.forEach($scope.damageEvants, function(event) { 
     if (!event.EndDate && !event.StartDate) { 
      var idx = $scope.damageEvants.indexOf(event); 
      $scope.damageEvants.splice(idx, 1); 
     } 
     }); 
    } 

基於舒尚特的回答另一種選擇是在你的NG-重複列表應用過濾器這樣的:

https://jsfiddle.net/qj7dz2eq/

控制器

$scope.filterEvents = function(item) { 
    return item.StartDate !== null || item.EndDate !== null; 
} 

HTML

<ul> 
    <li ng-repeat="item in damageEvants | filter:filterEvents">{{item.id}}</li> 
</ul> 

否則它的這些任何一種途徑防止被綁定到$範圍的兩倍相同的陣列。

1

可以嘗試這樣的:

$scope.damageEvants = $scope.damageEvants.filter(function(obj) { 
    return !(obj.StartDate === obj.EndDate && obj.StartDate === null); 
});