首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多重控制的阵列的动态滤波

问具有多重控制的阵列的动态滤波
EN

Stack Overflow用户
提问于 2022-02-09 03:07:47
回答 1查看 378关注 0票数 2

我有一个使用用户输入值构造的数组,可以有任意长度:

代码语言:javascript
复制
var products = [
 {key1: string1,
  key2: 1,
  key3: 30,
  key4: 1},

 {key1: string2,
  key2: 2,
  key3: 35,
  key4: 2}
]

我有一些过滤器

代码语言:javascript
复制
var filters = {
  key2: 1,
  key3: {min: 20, max: 40},
  key4: 1,
}

我目前正在使用下面的函数来使用我的过滤器过滤产品

代码语言:javascript
复制
        function multiFilter(array, filters) {
            const filterKeys = Object.keys(filters);
            return array.filter((item) => {
                // flipped around, and item[key] forced to a string
                return filterKeys.every(key => !!~String(item[key]).indexOf(filters[key]));
            });
        }

        var filtered = multiFilter(products, filters);
        console.log(filtered);

当过滤器中的key3有一个确切的数字时,一切都按预期工作,但是当我尝试合并一个范围时,我遇到了一些问题。我将如何修改函数以允许key3 (过滤器)中的范围?

EN

回答 1

Stack Overflow用户

发布于 2022-02-09 05:52:27

也许这种方法是有用的

​

代码语言:javascript
复制
const products = [
 {key1: 'string0',key2: 0,key3: 10,key4: 1},
 {key1: 'string1',key2: 1,key3: 15,key4: 1},
 {key1: 'string2',key2: 2,key3: 20,key4: 1},
 {key1: 'string3',key2: 3,key3: 25,key4: 2},
 {key1: 'string4',key2: 4,key3: 30,key4: 2},
 {key1: 'string5',key2: 5,key3: 35,key4: 2},
 {key1: 'string6',key2: 6,key3: 40,key4: 4},
 {key1: 'string7',key2: 7,key3: 45,key4: 4},
 {key1: 'string8',key2: 8,key3: 50,key4: 4},
 {key1: 'string9',key2: 9,key3: 55,key4: 4}];
 
const filters = {
  key2: {max: 7},
  key3: {min: 20, max: 60},
  key4: 4,
};

const filterProdacts = (products, filters) => {
// First, we create an object where the property values (min, max, eq) 
// are predicate functions.
  const fns = {
    eq: (key, value) => (obj) => obj[key] === value,
    min: (key, value) => (obj) => obj[key] >= value,
    max: (key, value) => (obj) => obj[key] <= value,
  };

// Generates a predicate based on the conditions
  makePredicat = (filterKey, filterValue) => {

// For consistency мake the numeric values as an object like { eq: 5 }
    const filterObj = (typeof filterValue === 'object') 
      ? filterValue
      : { eq: filterValue };
      
// Pick a function from fns and initialize it with the condition parameters
    return Object.entries(filterObj)
      .map(([condKey, condValue]) => fns[condKey](filterKey, condValue));
  };
  
// Fill in the array with predicate functions for each condition. 
// In this example there will be 4 functions, one for each condition 
// key2 - max: 7; key3 - min: 20; key3 - max: 60; key4 - eq: 4;
const predicats = Object.entries(filters).flatMap(([key, value]) => makePredicat(key, value));

// Initializing reduce by source array 
// then filtering it with all predicate functions
return predicats.reduce((acc, fn) => acc.filter(fn), products);
};

console.log(filterProdacts(products, filters));
代码语言:javascript
复制
.as-console-wrapper{min-height: 100%!important; top: 0}

​

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71043538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档