简体   繁体   English

如何在具有多个条件数组的javascript中链接多个过滤器函数

[英]How to chain multiple filter functions in javascript with multiple conditions array

I have an array of data containing multiple properties, that needs to be filtered out via select boxes. 我有一个包含多个属性的数据数组,需要通过选择框将其过滤掉。 Just like an eCommerce site where you can select a category, then certain sizes, then prices, then colors, etc.etc. 就像电子商务网站一样,您可以在其中选择类别,然后选择特定大小,价格,颜色等。 all the filters combined, give a result. 所有过滤器组合在一起,得出结果。

How can I filter down on multiple properties and eventually return data that only contains the selected values? 如何筛选多个属性并最终返回仅包含所选值的数据? http://jsfiddle.net/Jeffwise/z2y41k85/3/ http://jsfiddle.net/Jeffwise/z2y41k85/3/

var vm = new Vue({
  el: '#root',
  data: {
    list: [{
        name: 'asd',
        category: 1
      },
      {
        name: 'zxc',
        category: 1
      },
      {
        name: 'qwe',
        category: 1
      },
      {
        name: 'qqq',
        category: 2
      },
      {
        name: 'www',
        category: 2
      },
      {
        name: 'eee',
        category: 2
      },
      {
        name: 'rrr',
        category: 2
      },
      {
        name: 'ttt',
        category: 2
      },
      {
        name: 'ert',
        category: 1
      },
      {
        name: 'wer',
        category: 2
      },
      {
        name: 'sdf',
        category: 1
      },
      {
        name: 'dfg',
        category: 2
      },
      {
        name: 'xcv',
        category: 1
      }
    ],
    categories: [{
        id: 1,
        name: 'cat 1'
      },
      {
        id: 2,
        name: 'cat 2'
      }
    ],
    keyword: 'e',
    category: 1
  },

  computed: {
    filteredByAll() {
      return getByCategory(getByKeyword(this.list, this.keyword), this.category)
    },
    filteredByKeyword() {
      return getByKeyword(this.list, this.keyword)
    },
    filteredByCategory() {
      return getByCategory(this.list, this.category)
    }
  }
});

function getByKeyword(list, keyword) {
  const search = keyword.trim()
  if (!search.length) return list
  return list.filter(item => item.name.indexOf(search) > -1)
}

function getByCategory(list, category) {
  if (!category) return list
  return list.filter(item => item.category === category)
}

I think what I am searching for is chaining multiple filtered functions together. 我认为我正在寻找的是将多个过滤功能链接在一起。 But how do you do that? 但是,你是怎么做的? This js fiddle comes close, but how can you add more filters to this, so you can check for more functions/filters? 这个js小提琴非常接近,但是如何为它添加更多过滤器,以便可以检查更多的功能/过滤器? I have a total of 10 filters that should 'work' together. 我总共有10个过滤器,应该一起“工作”。 I'm an absolute beginner and still learning. 我是一个绝对的初学者,还在学习。

Of course there are several ways to solve this problem. 当然,有几种方法可以解决此问题。

Instead of chaining (eg data.withKey('cheap').withBrand('Levis') - which requires a new class with chainable methods on it) I would go with a recursive method that takes one filterObject. 与其链接(例如, data.withKey('cheap').withBrand('Levis') -需要一个带有可链接方法的新类),我将采用一种采用一个filterObject的递归方法。 Every key: value pair is a filter: 每个key: value对都是一个过滤器:

methods: {
  filter(data, filters) {
    // filters should be an object of filterOptions, e.g. { name: 'blue', weight: 20 }
    const filterProp = Object.keys(filters)[0]; // get first prop to filter for

    // filter your data by this prop and the corresponding value
    const filteredData = data.filter((entry) => {
      return entry[filterProp] === filters[filterProp]
    });

    // get all the other filters through dynamic destructuring
    const { [filterProp]: deletedFilter, ...otherFilters } = filters;

    // if there are no other filters return filteredData,
    // else filter filteredData again with the remaining filters
    return Object.keys(otherFilters).length <= 0
      ? filteredData : this.filter(filteredData, otherFilters);
  },
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM