0

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?

1 Answer 1

2

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";
    });
};
Sign up to request clarification or add additional context in comments.

Comments

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.