简体   繁体   English

按字母顺序过滤 - vuejs,axios

[英]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".我现在想通过“product.name”按字母顺序过滤这些结果。 Below is my axios functions.下面是我的 axios 函数。

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!感谢帮助!

You can use the sort method to sort the array of objects by the name property.您可以使用 sort 方法按名称属性对对象数组进行排序。

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

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

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