我有这个对象
$scope.items = [
{id:1, name:'John'},
{id:2, name:'Steve'},
{id:3, name:'Joey'},
{id:4, name:'Mary'},
{id:5, name:'Marylin'}];
我需要过滤两个值,如3,4,我正在尝试类似的东西
$scope.items2 = $filter('filter')($scope.items, {id:3,4});
我该怎么做?需要使用自定义过滤器吗?
答案 0 :(得分:2)
使用回调函数:
$scope.items2 = $filter('filter')($scope.items, function(obj) {
return obj.id == 3 || obj.id == 4;
});
或者只是以同样的方式使用普通的Array.prototype.filter
:
$scope.items2 = $scope.items.filter(function(obj) {
return obj.id == 3 || obj.id == 4;
});