Amy Amy - 1 month ago 13
AngularJS Question

filter array data in angular

$scope.currentEvent = Object { item_id: "10535", name: "johnny", user_type: "1",category_id: "5"}, Object { item_id: "10534", name: "smith", user_type: "1",category_id: "6"}, Object { item_id: "10536", name: "Greg", user_type: "1",category_id: "7"};


$scope.users = Object { item_id: "11355", name: "mathew", user_type: "1",category_id: "7"}, Object { item_id: "10336", name: "Greg", user_type: "2",category_id: "7"}, Object { item_id: "10545", name: "powell", user_type: "1",category_id: "7"}, Object { item_id: "10456", name: "micheal", user_type: "3",category_id: "7"};


Need to filter base on user_type

Expected result = $scope.validUser = Object { item_id: "11355", name: "mathew", user_type: "1",category_id: "7"}, Object { item_id: "10545", name: "powell", user_type: "1",category_id: "7"};


My work
I used the below code doesn't work for me

$scope.validUser = $scope.validUser.filter(function($scope.currentEvent){
return $scope.validUser.user_type === $scope.currentEvent.user_type;
});


Please guide How can we fix it out.

Thanks in adavance
Please take a look and also explain the way we can get the result
how to filter data in angularjs

Answer Source
$scope.validUser = $scope.users.filter(function(obj){
    return obj.user_type === $scope.currentEvent.user_type;
});
  • Filter on the users array not validUser.
  • The filter argument is the object for the current iteration, so don't use $scope there.