以下代码模仿http://blog.csdn.net/l5984652...
const ProductFilters = {
rangesFilter: function (products, ranges) {
if (ranges.length === 0) {
return products;
} else {
/**
* 循环多个区间条件,
* 每种区间类型应该只有一个,
* 比如价格区间不会有1000-2000和4000-6000同时需要的情况
*/
for (let range of ranges) {
if (range.low && range.high){
products = products.filter(function (item) {
return parseInt(item[range.type]) >= parseInt(range.low) && parseInt(item[range.type]) <= parseInt(range.high);
});
}
// 多个不同类型区间是与逻辑,可以直接赋值给自身
}
return products;
}
},
choosesFilter: function (products, chooses) {
let tmpProducts = [];
if (chooses.length === 0) {
tmpProducts = products;
} else {
/**
* 选择类型条件是或逻辑,使用数组连接concat
*/
for (let choice of chooses) {
tmpProducts = tmpProducts.concat(products.filter(function (item) {
return item[choice.type].indexOf(choice.value) !== -1;
}));
}
}
return tmpProducts;
}
}
const products = [
{name: '张山',sex: '男',age:'4',address: '江苏南京 ',identity: '南京',modifyDate:'2018-03-02',modifyTime: '09:33'},
{name: '张山',sex: '女',age:'8',address: '江苏苏州',identity: '苏州',modifyDate:'2018-03-03',modifyTime: '09:33'},
{name: '张山',sex: '男',age:'22',address: '江苏南京',identity: '南京',modifyDate:'2018-03-04',modifyTime: '09:33'},
{name: '找六',sex: '男',age:'14',address: '云南云商',identity: '云商',modifyDate:'2016-03-05',modifyTime: '09:33'},
{name: 'Rain',sex: '男',age:'42',address: '浙江嘉兴',identity: '嘉兴',modifyDate:'2017-03-06',modifyTime: '09:33'},
{name: 'MAXMAN',sex: '女',age:'26',address: '浙江乌镇',identity: '乌镇',modifyDate:'2018-03-07',modifyTime: '09:33'},
{name: '王六',sex: '男',age:'17',address: '江苏南京',identity: '南京',modifyDate:'2018-03-08',modifyTime: '09:33'},
{name: '李字',sex: '男',age:'67',address: '浙江杭州',identity: '杭州',modifyDate:'2018-03-09',modifyTime: '09:33'},
{name: '李四',sex: '男',age:'41',address: '湖南长沙',identity: '长沙',modifyDate:'2018-03-10',modifyTime: '09:33'},
{name: 'aaaaaa',sex: '女',age:'32',address: '湖北武汉',identity: '武汉',modifyDate:'2018-03-11',modifyTime: '09:33'},
{name: 'tttttt',sex: '男',age:'28',address: '湖南襄阳',identity: '襄阳',modifyDate:'2018-03-12',modifyTime: '09:33'}
];
let Conditions = {
ranges: [
{
type: 'age',
low: 4,
high: 18
}
],
chooses: [
{
type: 'name',
value: '张山'
},
{
type:'identity',
value:'南京'
}
]
};
function doFilter(products, conditions) {
// 根据条件循环调用筛选器里的方法
for (key in conditions) {
// 判断是否有需要的过滤方法
if (ProductFilters.hasOwnProperty(key + 'Filter') && typeof ProductFilters[key + 'Filter'] === 'function') {
products = ProductFilters[key + 'Filter'](products, Conditions[key]);
}
}
return products;
}
let result = doFilter(products, Conditions);
console.log(result)
上述图片显示的是逻辑或的选择类型条件,我想获取逻辑与的选择类型条件,应该怎么写呢