0

I have to filter based on two values. I am trying with following functionality

JSON data is,

{"homeAddress":"26, New Street, Bangalore",
        "officeAddress":"31, Old Office Street, Bangalore"
},{
"homeAddress":"27, Neww Street, Bangalore",
        "officeAddress":"30, Old Office Street, Bangalore"
}


        var locationFilter = {officeAddress: location};
        var locationFilter1 = {homeAddress: location};
        filterData = $filter('filter')(data, locationFilter);
        filterData = $filter('filter')(data, locationFilter1);
        return filterData;

By using above functionality. It have filtered second option only.

So how to filter with the two condition with the above styling and how can i use OR, AND Opeartion in this.

When i filtering For ex: 26 & 31 is filter correctly. But here 27 is not filtering

1 Answer 1

1

For AND functionality (i.e., when you want to show only the elements that have both officeAddress and homeAddress equal to location), just create a combined object and pass it into filter:

var bothLocationsCheck = {
  officeAddress: location,
  homeAddress: location
};
filterData = $filter('filter')(data, bothLocationsCheck);

That'll obviously give you 'AND'; for 'OR', just concat two results:

filteredData = $filter('filter')(data, locationFilter).concat(
  $filter('filter')(data, locationFilter1));

... but in this case you'll have to remove the duplicates from the result.

Still, I'd prefer another approach: implementing this kind of operations within filtering function itself. For example:

filteredData = $filter('filter')(data, function(el) {
  return el.homeAddress.indexOf(location) !== -1  
      || el.officeAddress.indexOf(location) !== -1
});

It's rather trivial to change this function from OR to AND functionality.

Sign up to request clarification or add additional context in comments.

3 Comments

I have to use OR Operation. But it may give AND Operation
Could you be more specific about which of those not working in your case?
Data has filtered from officeAddress but not in homeAddress

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.