【问题标题】:Filtering API response using axios with Vue.js使用带有 Vue.js 的 axios 过滤 API 响应
【发布时间】:2021-10-13 05:56:44
【问题描述】:
// this is my method    
filterProducts() {
      axios
        .get("http://fake-api.ns.nsdevel.eu/products")
        .then((resp3) => {this.products = resp3.data.products})
        .catch((error) => {
          this.errorMessage = error.message;
          console.error("There was an error!", error);
        });
    },

我想根据公司过滤这些产品。有人可以给我一个提示吗?试了很多次都没用。

【问题讨论】:

    标签: api vue.js axios


    【解决方案1】:
    filterProducts(companyName) {
          axios
            .get("http://fake-api.ns.nsdevel.eu/products")
            .then((resp3) => {this.products = resp3.data.products.filter(x => x.company.name == companyName)})
            .catch((error) => {
              this.errorMessage = error.message;
              console.error("There was an error!", error);
            });
        }
    

    【讨论】:

    • 如果我不仅要过滤公司还要过滤类别?
    • 假设你有一个categoryIds参数,你可以这样做:resp3.data.products.filter(x => x.company.name == companyName || x.categories.some(cat => categoryIds.includes(cat.id)))
    猜你喜欢
    • 2018-08-02
    • 2021-10-17
    • 2020-12-25
    • 2019-04-29
    • 1970-01-01
    • 2020-12-22
    • 2022-01-25
    • 2023-02-23
    • 2021-11-17
    相关资源
    最近更新 更多