Home > database >  filtering by alphabetical order - vuejs, axios
filtering by alphabetical order - vuejs, axios

Time:11-18

I already have a filter that filters by brand name. I now want to filter these results by alphabetical order through "product.name". Below is my axios functions.

export default {
  data() {
    return {
      filteredProducts: []
    };
  },
  mounted() {
 axios
 .get('/src/stores/sneakers.json')
 .then(response => (this.filteredProducts = response.data))
  },

  computed: {
    resultCount () {
            return Object.keys(this.filteredList).length
        },
    filteredList(){
            return this.filteredProducts.filter(product => 
            product.brand.includes(this.brand?.name)
      )
        }
    },

Help is appreciated!

CodePudding user response:

You can use the sort method to sort the array of objects by the name property.

this.filteredProducts.sort((a, b) => {
  if (a.name < b.name) {
    return -1;
  }
  if (a.name > b.name) {
    return 1;
  }
  return 0;
});
  • Related