在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ js 根據(jù)多條件篩選數(shù)組數(shù)據(jù)

js 根據(jù)多條件篩選數(shù)組數(shù)據(jù)

以下代碼模仿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)該怎么寫呢

回答
編輯回答
情皺
/*邏輯與的實現(xiàn),其實邏輯與就是多次過濾,滿足所有的條件,最后的結(jié)果*/
tmpProducts=products;
for(let choice of chooses){
    if (tmpProducts.length === 0 ) return tmpProducts;
    tmpProducts.filter(function(item){return item[choice.type].indexOf(choice.value) !== -1 });
}
return tmpProducts;
2018年8月19日 23:22
編輯回答
夕顏
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;
 }
2018年1月26日 10:07
編輯回答
笑忘初
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)

clipboard.png

2018年1月17日 21:13