Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filtering by alphabetical order – vuejs, axios

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading