使用 JavaScript 方法对项目进行排序

sorting items using javascript method

提问人:Mihlali 97 提问时间:9/12/2023 更新时间:9/12/2023 访问量:38

问:

我正在尝试使用排序方法,但它一直告诉我未定义 filteredProducts

用于对项目进行排序的代码

data() {
    return {
      MovieCategory: ["Action", "Adventure", "Family", "Comedy", "All"],
      filteredProducts: [],
    };
  methods: {
    filterMovies(category) {
      if (category == "All") {
        this.filteredProducts = this.products;
      } else {
        this.filteredProducts = this.products.filter(
          (product) => product.MovieCategory === category
        );
      }
    },
  sortItems() {
      this.filteredProducts = this.filteredProducts.sort((a, b) => {
        if (a.MovieCategory < b.MovieCategory) {
          return -1;
        } else {
          return 1;
        }
      });
    },
JavaScript 方法 vuejs3

评论

0赞 Keith 9/12/2023
这很可能是一个绑定问题,<---并且没有 ,但如果没有更多细节,很难说。methods: {methodsfilteredProducts
0赞 Bennison J 9/12/2023
记录对象“this”,并检查它是否具有属性“filteredProducts”。
0赞 GrafiCode 9/12/2023
this.filteredProducts = this.products什么?this.products
3赞 traktor 9/12/2023
请将发布的代码转换为带有更多代码的代码片段(在问题本身中),以便可以运行该代码片段来演示错误。
0赞 Wimanicesir 9/12/2023
您无需分配排序。它会自我调整。因此,删除 this.filteredProducts 作为行的开头:'this.filteredProducts = this.filteredProducts.sort'

答: 暂无答案