Angular filter a object by its properties Angular filter a object by its properties angularjs angularjs

Angular filter a object by its properties


Little late to answer, but this might help :

If you want to filter on a grandchild (or deeper) of the given object, you can continue to build out your object hierarchy. For example, if you want to filter on 'thing.properties.title', you can do the following:

<div ng-repeat="thing in things | filter: { properties: { title: title_filter } }">

You can also filter on multiple properties of an object just by adding them to your filter object:

<div ng-repeat="thing in things | filter: { properties: { title: title_filter, id: id_filter } }">

The syntax of 'not equals' is just a little off, try the following:

<div ng-repeat="thing in things | filter: { properties: { title: '!' + title_filter } }">


Although I agree that converting your object might be the best option here, you can also use this filter function:

angular.module('app').filter('objectByKeyValFilter', function () {return function (input, filterKey, filterVal) {    var filteredInput ={};     angular.forEach(input, function(value, key){       if(value[filterKey] && value[filterKey] !== filterVal){          filteredInput[key]= value;        }     });     return filteredInput;}});

like this:

<div ng-repeat="(key, value) in data | objectByKeyValFilter:'type':'foo'">{{key}}{{value.type}}</div> 

See also the Plunker.


Filter works on arrays but you have an object literal.

So you can either convert your object literal into an array or create your own filter than takes in the object literal.

If you don't need those index values then converting to an array may be your best bet( Here's a fiddle with the array working: http://jsfiddle.net/NqA8d/3/):

$scope.items = [{    "type": "foo",        "name": "blah"}, {    "type": "bar"}, {    "type": "foo"}, {    "type": "baz"}, {    "type": "test"}];

In case you'd like to do a filter, here's one way to do that:

myApp.filter('myFilter', function () {    return function (items, search) {        var result = [];        angular.forEach(items, function (value, key) {            angular.forEach(value, function (value2, key2) {                if (value2 === search) {                    result.push(value2);                }            })        });        return result;    }});

And that fiddle: http://jsfiddle.net/NqA8d/5/