TietjeDK
TietjeDK

Reputation: 1207

Angular dynamic filter for array

I'm trying to create a filtered array with dynamic variables. I create an array which holdes the filter keys, and then I create an filtered array which only should return items that match the keys from my first array.

Array with filter keys: $scope.participantArray = ["[email protected]", "[email protected]"]

Code for filtering second array:

$scope.items = $scope.items.filter(function (data) {
                            var i = $scope.participantArray.length;
                             while( i-- ) {
                           return  ( data.Title === $scope.participantArray[i] ) 
                        }

I'm trying to loop through all keys and apply them to the filtered array. The problem is that it only returns one match. I ahve to instances in my items array which match the keys from my first array.

The while loop only returns [email protected].

Any suggestions on what i am doing wrong?

Upvotes: 0

Views: 83

Answers (1)

Tarun Dugar
Tarun Dugar

Reputation: 8971

You can do it in an easier way using indexOf:

$scope.items.filter(function(item) {
    if($scope.participantArray.indexOf(item.Title) >= 0) {
        return true;
    }
})

Upvotes: 3

Related Questions