0

I have the following angular object in my controller

$scope.students = [{:id => 1, :state => 'active'}, {:id => 2, :state => 'suspended'}]

How can i get id of all students whose state is active? Can anyone please help me with this?

1 Answer 1

1

There is multiple ways but the easiest I can think of is adding an activeStudent function in your scope, something like this:

$scope.activeStudent = function(){
  return _.filter($scope.students, function(s){
    return s.state == "active";
  });
}

and in your views you would use activeStudent() in your binding.

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

2 Comments

Thanks Very much Schmurfy! Is there a better way to get the ids of all the students instead of looping and collecting id? like $scope.students.collect(&:id) Thanks again!
I use underscore with angularjs and I am pretty happy with it (I used it in the example), you can look at underscorejs.org/#map and underscorejs.org/#pluck .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.