【问题标题】:Filtering through dropdwon in javascript not throwing any result after use a forEach and then filter query在使用forEach然后过滤查询后通过javascript中的下拉列表进行过滤不会抛出任何结果
【发布时间】:2020-04-10 14:47:55
【问题描述】:

开发者好,我正在开发这个应用程序,现在正在使用下拉列表过滤元素,查询 json 对象并将其与下拉列表中的选定元素进行比较。 但是任何时候我只选择一个元素它都不会显示任何结果。 这是我的 json 对象,它由一个名为“getAllProducts”的 getter 访问:

{
   "user":null,
   "products":[
      {
         "product_name":"Chain Saw",
         "product_category":[
            {
               "categories_of_product":"Good"
            },
            {
               "categories_of_product":"Danger"
            },
            {
               "categories_of_product":"Homer"
            }
         ]
      },
      {
         "product_name":"Chuky",
         "product_category":[
            {
               "categories_of_product":"Danger"
            },
            {
               "categories_of_product":"Homer"
            }
         ]
      },
      {
         "product_name":"Glasses",
         "product_category":[
            {
               "categories_of_product":"Good"
            },
            {
               "categories_of_product":"Homer"
            }
         ]
      },
      {
         "product_name":"Tile",
         "product_category":[
            {
               "categories_of_product":"Horror"
            },
            {
               "categories_of_product":"Homer"
            }
         ]
      },
      {
         "product_name":"Mouse",
         "product_category":[
            {
               "categories_of_product":"Homer"
            }
         ]
      },
      {
         "product_name":"rino",
         "product_category":[
            {
               "categories_of_product":"Zoo"
            },
            {
               "categories_of_product":"Park"
            }
         ]
      },
      {
         "product_name":"Chain Saw",
         "product_category":[
            {
               "categories_of_product":"Homer"
            },
            {
               "categories_of_product":"Horror"
            }
         ]
      },
      {
         "product_name":"Chain Saw1",
         "product_category":[
            {
               "categories_of_product":"Homer"
            },
            {
               "categories_of_product":"Good"
            }
         ]
      },
      {
         "product_name":"Chain Saw2",
         "product_category":[
            {
               "categories_of_product":"Good"
            }
         ]
      },
      {
         "product_name":"rino1",
         "product_category":[
            {
               "categories_of_product":"Zoo"
            },
            {
               "categories_of_product":"Animal"
            }
         ]
      }
   ]
}

然后在我的方法中,我声明了一个函数,该函数以前针对 v 建模的全局变量,该变量遵循我的 html 中的某个标记,该标记是从其中一个应用程序组件导入的(所有这些都很好)。让我们说:

SCRIPT

data(){
 return{
     CategoriesDropDown: "",
 }
},
methods:{
 filterSearch(selectedCategory) {
      this.CategoriesDropDown = selectedCategory;
 },
}

然后在我的最后一步,当我尝试使用 this.CategoriesDropDown 过滤所有元素时,它不起作用,只需选择我的 dropDown 的任何类别,我的所有产品都会消失。在这里我设置了访问的功能json 对象与下拉列表中的选定选项进行比较应该过滤但不起作用:

COMPUTED

   callProducts() {
       if (this.CategoriesDropDown) {
        return this.getAllProducts.products.forEach(categories => {
          return categories.product_category.filter(string => {
            return  string.categories_of_product.toUpperCase() === this.CategoriesDropDown.toUpperCase()
            ;
          });
        });
      } else {
        return this.getAllProducts.products;
    }
  },

当我没有选择任何东西时

选择任何一个项目时在此处

关于如何改进最后一个功能以正确过滤我的产品的任何建议?提前致谢!!!

【问题讨论】:

    标签: javascript vue.js for-loop foreach


    【解决方案1】:

    this.getAllProducts.products.forEach(...) 中的forEach 替换为filter

    像这样:

    return this.getAllProducts.products.filter(categories => { // return only some products
      return categories.product_category.filter(string => {
        return  string.categories_of_product.toUpperCase() === this.CategoriesDropDown.toUpperCase()
      }).length !== 0 // check that product has selected category
    });
    

    【讨论】:

    • 为了访问类别,我的 jason 的结构是以这种方式组成的……我的意思是数组,然后是第二个数组,其中包含类别的对象。设置地图会给我更多错误
    • 真的很感谢 Maxim ...这是我的第一次尝试,但没有在函数末尾使用 .length!==0 布尔值。感谢您的帮助!!!!祝您有美好的一天! !!
    • 此解决方案不起作用,您每次都会获得所有产品。如果没有找到并且 [] 为真,则第二个过滤器将返回一个空数组。所以第一个过滤方法将始终被测试为 true 并返回所有项目。
    • 我的坏@MaximMazurok,我没有看到 .lenght !== 0
    【解决方案2】:

    Array.prototype.forEach 返回undefined。永远!

    https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/forEach

    所以你必须重新考虑你的代码以避免这种forEach

     callProducts() {
           if (this.CategoriesDropDown) {
            return this.getAllProducts.products.filter(categories => {
              return categories.product_category.some((string => {
                return  string.categories_of_product.toUpperCase() === this.CategoriesDropDown.toUpperCase()
              })
            });
          } else {
            return this.getAllProducts.products;
        }
      }
    

    【讨论】:

    • 非常感谢您的回复 Romain,它也可以完美运行...将保留它以供将来的任务使用。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2012-08-05
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多