0

I get this filter:

userInfo = $filter('filter')(userArray, {userId: userId});

and got the userInfo object. Is there anyway that I can find out which array index of userArray. Thanks

1

1 Answer 1

0

Following the docs, you can see that you can use a function instead of the object {userId: userId}. https://docs.angularjs.org/api/ng/filter/filter#filter-arguments

Right now your code should be equivalent to the following:

userInfo = $filter('filter')(userArray, function(element, index) {
    if (element.userId == userId) return true;
});

Note that the function added to the filter contains the index that you need.

You may change the function to make use of that index, but bear in mind that you must return true only for the elements that you want to pass the filter.

Sign up to request clarification or add additional context in comments.

2 Comments

You are welcome! But don't forget to mark my answer as correct! :-)
Sorry, Another question. I really want to return the index because I need to use this index to update the data outside of this function(function(element, index) lll

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.