我必须根据两个值进行过滤。我正在尝试以下功能
JSON数据是,
{"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;
使用上述功能。它仅过滤了第二个选项。
那么如何使用上述样式过滤两个条件以及如何在此使用OR,AND Opeartion。
当我过滤时为ex:26& 31正确过滤。但这里27不是过滤
答案 0 :(得分:1)
对于AND
功能(即,当您只想显示同时officeAddress
和homeAddress
等于location
的元素时),只需创建一个组合对象,将其传递给filter
:
var bothLocationsCheck = {
officeAddress: location,
homeAddress: location
};
filterData = $filter('filter')(data, bothLocationsCheck);
那显然会给你'和';对于'OR',只需得到两个结果:
filteredData = $filter('filter')(data, locationFilter).concat(
$filter('filter')(data, locationFilter1));
...但在这种情况下,您必须从结果中删除重复项。
不过,我更喜欢另一种方法:在过滤功能本身内实现这种操作。例如:
filteredData = $filter('filter')(data, function(el) {
return el.homeAddress.indexOf(location) !== -1
|| el.officeAddress.indexOf(location) !== -1
});
将此功能从OR
更改为AND
功能非常简单。