0

I have an array of leads in my component but need to filter out all of the leads where selected is equal to true and store them in a variable.

How would I do this with AngularJS?

This is what I currently have but it is coming back as undefined:

var list = $filter("filter")($scope.leads, {
            selected: true
});

Note that some objects have the property: "selected": true and some don't.

Please let me know what (if any) further info you require.

Thanks

5
  • What value does $scope.leads have? Commented May 21, 2018 at 14:15
  • Do you use true or "true" values? Boolean and strings are different Commented May 21, 2018 at 14:16
  • Boolean. Sorry, have amended the above. Thanks Commented May 21, 2018 at 14:21
  • @Andrew It has an array of lead objects that container a variety of properties including the selected: true / false property Commented May 21, 2018 at 14:24
  • Are you injecting $filter in to the controller? Commented May 21, 2018 at 14:28

2 Answers 2

1

I made an example of how to filter by selected. Take a look.

var leads=[
{"name":"one","selected":true},
{"name":"two","selected":false},
{"name":"three"},
{"name":"four","selected":true}
];

var list = leads.filter(function(lead) {
           return lead.selected
});

console.log(list);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

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

1 Comment

I thought using the angular filter service was a requirement. This is using the Array.filter method.
0

Can't reproduce problem.

angular.module("app",[])
.run(function($filter) {
    var $scope_leads=[
    {"name":"one","selected":true},
    {"name":"two","selected":false},
    {"name":"three"},
    {"name":"four","selected":true}
    ];

    var list = $filter("filter")($scope_leads, {
                selected: true
    });
    console.log(list);
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<body>

1 Comment

Not sure why it wasn't working either! But the below worked! thanks for taking the time to help though.

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.