javascript - 过滤数据数组多个值javascript

标签 javascript arrays

我知道以前有人问过这个问题,但现有的解决方案似乎不适用于我的情况。我正在尝试根据各种对象数组中的多个属性/值过滤掉数据。

我的示例数据如下所示:

const products = [
          { name: 'A', color: 'Blue', size: 50, locations: 'USA' },
          { name: 'B', color: 'Blue', size: 60, locations: 'Europe' },
          { name: 'C', color: 'Blue', size: 30, locations: 'Europe' },
          { name: 'D', color: 'Black', size: 70, locations: 'Japan' },
          { name: 'E', color: 'Green', size: 50, locations: 'Europe' },
          { name: 'F', color: 'Blue', size: 50, locations: 'Brazil' },
          { name: 'G', color: 'Black', size: 40, locations: 'Australia' },
        ];

这就是我要求的过滤器结果:

const filters_one = ['Blue'];

const requiredResult_One = [
      { name: 'A', color: 'Blue', size: 50, locations: 'USA' }, 
      { name: 'B', color: 'Blue', size: 60, locations: 'Europe' },
      { name: 'C', color: 'Blue', size: 30, locations: 'Europe' },
      { name: 'E', color: 'Blue', size: 50, locations: 'Brazil' }, 
]


const filters_two = ['Blue', 'Europe'];

const requiredResult_Two = [
      { name: 'B', color: 'Blue', size: 60, locations: 'Europe' },
      { name: 'C', color: 'Blue', size: 30, locations: 'Europe' }, 
]


----------OR-------------------------
const filters_three = ['Black'];

const requiredResult_three = [
      { name: 'D', color: 'Black', size: 70, locations: 'Japan' },
      { name: 'G', color: 'Black', size: 40, locations: 'Australia' }, 
]

const filters_four = ['Black', 'Japan'];

const requiredResult_Four = [
      { name: 'D', color: 'Black', size: 70, locations: 'Japan' },
]

这是我迄今为止所取得的成就:

const filterdata = (products, filter) => {
      // where, filter can be equal to *filters_one*, *filters_two*, *filters_three*, 
         or *filters_four* anyone of it. //

      const keysExact = ['color', 'locations'];
      const valuesExact = filter.map(col => col.toLowerCase());

      const resultExact = products.filter((prod) =>
          keysExact.every((key) => valuesExact.includes(prod[key].toLowerCase()))
      );

      console.log(resultExact);
};

这似乎部分有效,或者对我来说似乎不是一个好方法。如果有人可以帮助我找到更好的解决方案,那将非常有帮助。

提前致谢!

最佳答案

您可以从对象中获取值并检查查询的所有值是否都在值数组中。

const
    filter = (data, query) => data.filter(o => {
        const values = Object.values(o);
        return query.every(q => values.includes(q));
    }),
    products = [{ name: 'A', color: 'Blue', size: 50, locations: 'USA' }, { name: 'B', color: 'Blue', size: 60, locations: 'Europe' }, { name: 'C', color: 'Blue', size: 30, locations: 'Europe' }, { name: 'D', color: 'Black', size: 70, locations: 'Japan' }, { name: 'E', color: 'Green', size: 50, locations: 'Europe' }, { name: 'F', color: 'Blue', size: 50, locations: 'Brazil' }, { name: 'G', color: 'Black', size: 40, locations: 'Australia' }];

console.log(filter(products, ['Blue']));
console.log(filter(products, ['Blue', 'Europe']));
console.log(filter(products, ['Black']));
console.log(filter(products, ['Black', 'Japan']));
console.log(filter(products, [['Blue', 'Green']]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

同一键的替代值的一种方法。

const
    filter = (data, query) => data.filter(o => {
        const values = Object.values(o);
        return query.every(q =>
            values.includes(q) ||
            Array.isArray(q) && q.some(v => values.includes(v))
        );
    }),
    products = [{ name: 'A', color: 'Blue', size: 50, locations: 'USA' }, { name: 'B', color: 'Blue', size: 60, locations: 'Europe' }, { name: 'C', color: 'Blue', size: 30, locations: 'Europe' }, { name: 'D', color: 'Black', size: 70, locations: 'Japan' }, { name: 'E', color: 'Green', size: 50, locations: 'Europe' }, { name: 'F', color: 'Blue', size: 50, locations: 'Brazil' }, { name: 'G', color: 'Black', size: 40, locations: 'Australia' }];

console.log(filter(products, ['Blue']));
console.log(filter(products, [['Blue', 'Green']]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 过滤数据数组多个值javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70334082/

相关文章:

javascript - 将for循环附加到html中的div

javascript - 你能简化和概括这个有用的 jQuery 函数吗?

javascript - 使用 Google Analytics 和 Facebook Pixel 的 Closure Compiler 警告

c++ - C 与 C++ 对象的静态初始化

python - 使用带有稀疏 scipy 矩阵的广播

javascript - 上传文件而不提交的脚本

arrays - 搜索整个数组 Swift

arrays - 在 shell 脚本中将多行输出解析为数组

c - 标记一个字符串以作为 char * 传递给 execve()

javascript - 在 DOM 元素上使用 Javascript 的 typeof 来检查未定义(IE 问题)