[Vuejs]-Chaining multiple filters in vuex getters

0👍

The getter could call Array.prototype.filter() on the input based on the given filters, one by one. The following example assumes filterByStatus, filterByCountry and filterByState each contain the search term.

const getters = {
    filteredData: state => {
       let bldgs = state.bldgList
       if (state.filterTypes.filterByStatus) {
           bldgs = bldgs.filter(b => b.status.includes(state.filterTypes.filterByStatus))
       }
       if (state.filterTypes.filterByCountry) {
           bldgs = bldgs.filter(b => b.country === state.filterTypes.filterByCountry)
       }
       if (state.filterTypes.filterByState) {
           bldgs = bldgs.filter(b => b.state === state.filterTypes.filterByState)
       }
       return bldgs
    }
};

Leave a comment