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">

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?

Problem courtesy of: Marjan Kalanaki


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">
Solution courtesy of: Ghassen Louhaichi


You can use something like:


    <div ng-repeat="item in vm.templateList | filter:checkFilterOptions">


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

    $scope.filterOptions = [1,2,3];

    $scope.checkFilterOptions = function(value, index) {
      return value.ID && $scope.filterOptions.indexOf(value.ID) !== -1;
Discussion courtesy of: Yoan

This recipe can be found in it's original form on Stack Over Flow.