簡體   English   中英

如何在具有多個條件數組的javascript中鏈接多個過濾器函數

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

我有一個包含多個屬性的數據數組,需要通過選擇框將其過濾掉。 就像電子商務網站一樣,您可以在其中選擇類別,然后選擇特定大小,價格,顏色等。 所有過濾器組合在一起,得出結果。

如何篩選多個屬性並最終返回僅包含所選值的數據? 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)
}

我認為我正在尋找的是將多個過濾功能鏈接在一起。 但是,你是怎么做的? 這個js小提琴非常接近,但是如何為它添加更多過濾器,以便可以檢查更多的功能/過濾器? 我總共有10個過濾器,應該一起“工作”。 我是一個絕對的初學者,還在學習。

當然,有幾種方法可以解決此問題。

與其鏈接(例如, data.withKey('cheap').withBrand('Levis') -需要一個帶有可鏈接方法的新類),我將采用一種采用一個filterObject的遞歸方法。 每個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