以下代碼模仿http://blog.csdn.net/l5984652...
const ProductFilters = {
rangesFilter: function (products, ranges) {
if (ranges.length === 0) {
return products;
} else {
/**
* 循環(huán)多個區(qū)間條件,
* 每種區(qū)間類型應(yīng)該只有一個,
* 比如價格區(qū)間不會有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);
});
}
// 多個不同類型區(qū)間是與邏輯,可以直接賦值給自身
}
return products;
}
},
choosesFilter: function (products, chooses) {
let tmpProducts = [];
if (chooses.length === 0) {
tmpProducts = products;
} else {
/**
* 選擇類型條件是或邏輯,使用數(shù)組連接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: '浙江烏鎮(zhèn)',identity: '烏鎮(zhèn)',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) {
// 根據(jù)條件循環(huán)調(diào)用篩選器里的方法
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)
上述圖片顯示的是邏輯或的選擇類型條件,我想獲取邏輯與的選擇類型條件,應(yīng)該怎么寫呢
choosesFilter: function (products, chooses) {
let tmpProducts = products;
// 選擇類型條件是或邏輯,這里不需要使用數(shù)組連接concat,一層一層依次篩選就好了
for (let choice of chooses) {
tmpProducts = tmpProducts.filter(function (item) {
return item[choice.type].indexOf(choice.value) !== -1;
});
}
return tmpProducts;
}const FilterType = {
range: (obj, column) => ({min, max}) => obj[column] >= min && obj[column] <= max,
match: (obj, column) => ({value}) => obj[column] === value,
like: (obj, column) => ({value}) => obj[column] && obj[column].indexOf(value) !== -1
}
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: '浙江烏鎮(zhèn)', identity: '烏鎮(zhèn)', 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 = {
age: { type: 'range', min: 4, max: 18 },
name: { type: 'match', value: '張山' },
address: { type: 'like', value: '江蘇' }
}
const doFilter = (products, conditions, cql) => {
const compile = cql.replace(/\w+/g, column => {
if (!Conditions[column]) { throw new Error(`column not found: ${column}`) }
else if (!FilterType[Conditions[column].type]) { throw new Error(`filterType not found: ${Conditions[column].type}`) }
return `F[C['${column}'].type](item, '${column}')(C['${column}'])`
})
const conditionCluster = new Function('F', 'C', `return item => ${compile}`)(FilterType, Conditions)
return products.filter(conditionCluster)
}
let result0 = doFilter(products, Conditions, 'age && name && address');
let result1 = doFilter(products, Conditions, 'age && name || address');
let result2 = doFilter(products, Conditions, 'age || name || address');
console.log(result0.length, result0)
console.log(result1.length, result1)
console.log(result2.length, result2)
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。