Felipe Leão
Felipe Leão

Reputation: 925

Passing complete object to AngularJS filter Comparator

I'm using angular's $filter("filter") to filter an array o objects. Since I have a complex filtering I decided to define a comparator. Nonetheless, inside my comparator only the attributes that appear on the filtering expression are available

var array = [
    {"id": 1, "placeId":253, "name":"John"},
    {"id": 2, "placeId":253, "name":"Jane"},
    {"id": 3, "placeId":32, "name":"Mike"},
    {"id": 4, "placeId":89, "name":"Ana"}
];

var awesomeFiltering = function(){
    return $filter("filter")(array, {"placeId":253}, function(actual expected){
        // "actual" brings only the value for placeId attribute!
        // How do I access attribute "name", for example?
    });
};

How can I acces the other attributes from the object inside the comparator?

Upvotes: 0

Views: 239

Answers (1)

Maak
Maak

Reputation: 5038

The comparator is used only for the values retrieved from your defined expression {"placeId":253}. Since your expression only includes placeId that is the only value you can retrieve in the comparator.

However you can just use a custom filter expression, without using a comparator for complex filtering.

var awesomeFiltering = function(){
    return $filter("filter")(array, function(value, index, array){
        return value.placeId === 253 && value.name === "John";
    });
};

Upvotes: 2

Related Questions