1

Below is my json array, and I'm trying to filter it based on active users.

var users = [{"name":"user1","isActive":true}, {"name":"user2","isActive":false}, {"name":"user3", "isActive":true}];

 $scope.filteredUsers = _.filter(users, function(user) { return user.isActive == true; });

I would want the output to be

 $scope.filteredUsers = [{"name":"user1","isActive":true}, {"name":"user3", "isActive":true}]; 

what am I doing wrong ? Is this the right approach or should I use _.where ? Thanks

4
  • It works. The _filteredUsers is exactly what you need it to be after running this code. Commented Mar 6, 2014 at 15:26
  • 1
    Isn't it return user.isActive == true;? Commented Mar 6, 2014 at 15:30
  • I've edited my question. Please see my comment in the answer below for more details about what I'm trying to do. Commented Mar 6, 2014 at 15:32
  • Why didn't you include that in your original question? Commented Mar 6, 2014 at 15:36

1 Answer 1

3

I cannot see any error in your approach except that you are filtering for isActive == false. If you want the active users you have to change your filter:

var activeUsers = _.filter(users, function(user) { return user.isActive });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've edited my question. I'm trying to use the same approach with in an angular controller. So, something like $scope.filteredItems = _.where(.....); It doesn't seem to work thus far. Let me check my code again.

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.