0

I am trying to insert values into an array one by one like java or c#.

var listOfProduct: IProduct[];
var product = new Array<IProduct>();

product.name= newName;
product.id= newId;
product.price= newPriceAfterDiscount;

this.listofproducts.push(product);

but typescript don't support that approach.

if there is another solution or idea for that i will be greatful. Thank you in advance

1 Answer 1

1

It should be:

var listOfProduct = [] as IProduct[];
var product = {} as IProduct;

product.name = newName;
product.id = newId;
product.price = newPriceAfterDiscount;

listOfProduct.push(product);

Or:

var listOfProduct = [] as IProduct[];
listOfProduct.push({
    name: newName,
    id: newId,
    price: newPriceAfterDiscount
});
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.