【问题标题】:vue.js matching array methodvue.js 匹配数组方法
【发布时间】:2021-04-09 21:50:42
【问题描述】:

所以我试图用 id_product == 输入的 myproduct 的值填写我的表单。但是当我运行代码时,该值不返回。我的代码有什么问题

this.products.forEach(i => {
       if(this.products[i].id == item.id_product)
        {
            
            this.form.product_name = this.products[i].product_name;
            
            this.form.id_category = this.products[i].id_category;
            this.form.description = this.products[i].description;
            this.form.price = this.products[i].price;
            this.form.color = this.products[i].color;
            this.form.size = this.products[i].size;
            this.form.stock = this.products[i].stock;
            this.form.weight = this.products[i].weight;
        } 
    });

【问题讨论】:

    标签: javascript arrays vue.js for-loop


    【解决方案1】:

    Array.forEach() 回调的第一个参数是当前值,而不是索引。第二个参数(可选)是索引,所以要么像.forEach(product => {...})那样只传递当前值,要么像.forEach((product, i) => {...})那样传递索引

    this.products.forEach(product => {
           if(product.id == item.id_product)
            {
                
                this.form.product_name = product.product_name;
                
                this.form.id_category = product.id_category;
                this.form.description = product.description;
                this.form.price = product.price;
                this.form.color = product.color;
                this.form.size = product.size;
                this.form.stock = product.stock;
                this.form.weight = product.weight;
            } 
        });
    
    

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 2016-12-02
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      相关资源
      最近更新 更多