0

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?

1 Answer 1

3

You can do it in an easier way using indexOf:

$scope.items.filter(function(item) {
    if($scope.participantArray.indexOf(item.Title) >= 0) {
        return true;
    }
})
Sign up to request clarification or add additional context in comments.

3 Comments

Great job! Nice and simple. Will mark as completed as soon as possible.
Missing ')' in your if statement.
yup, added that. Also, changed the condition to account for 0th index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.