0

so i'm trying to fill my form with myproduct's value where the id_product == input.But when i run the code,the value is not return.What's wrong with my code

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;
        } 
    });

1 Answer 1

1

First parameter of the callback for Array.forEach() is the current value, not index. Second parameter(optional) is index, so either pass the current value only like .forEach(product => {...}) or with index like .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;
        } 
    });

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.