Search code examples
angularjsangularjs-ng-repeatangularjs-filter

How to filter an array of objects based on an array of a specific object propert?


Here is a small example of my html using ng-repeat:

<div ng-repeat="item in vm.templateList | filter: vm.myFilter">
   <h3>{{item.Code}}</h3>
</div>

In Js file the vm.templateList is as followed(as an example):

vm.templateList = [{Code: 'a', ID: 1}, 
                   {code: 'a', ID: 2},
                   {code: 'b', ID: 3},
                   {code: 'c', ID: 4}];

Imagine I want to filter this list for all items that have ID 1 and also items that have ID 2.

What I originaly was doing was like this:

vm.filter = {ID: 1};

But this was I can only filter the list on 1 ID. Can anyone suggest a way?


Solution

  • You can add the following AngularJS filter to your application :

    // add a custom filter to your module
    angular.module('MyModule').filter('myFilter', function() {
        // the filter takes an additional input filterIDs
        return function(inputArray, filterIDs) {
            // filter your original array to return only the objects that
            // have their ID in the filterIDs array
            return inputArray.filter(function (entry) {
                return this.indexOf(entry.ID) !== -1;
            }, filterIDs); // filterIDs here is what "this" is referencing in the line above
        };
    });
    

    You then declare your filter array in the controller as such :

    vm.IDs = [1, 2];
    

    Then your view should look like this :

    <div ng-repeat="item in vm.templateList | myFilter: vm.IDs">
        <h3>{{item.Code}}</h3>
    </div>