Looking to fill up the orders
array, which consists of objects of type Order
. The desired output is
orders=[{id:1,qt:4},{id:2, qt:2},{id:3,qt:2}]
. How can I achieve this using TypeScript? I am new to this language.
export class Product {
constructor(public id: number, public name: string, public price: number) {}
}
export interface Order {
id: number;
qt: number;
}
export const products: Product[] = [
new Product(1, 'Apple', 2.1),
new Product(2, 'Banana', 2.2),
new Product(3, 'Chocolate', 2.3),
new Product(4, 'Dessert', 2.4),
];
export const cart: Product[] = [
products[0],
products[0],
products[2],
products[1],
products[2],
products[0],
products[1],
products[0],
];
export const orders: Order[] = [];
Edit
For those wondering how
orders=[{id:1,qt:4},{id:2, qt:2},{id:3,qt:2}]
is achieved.
In the cart
:
- the quantity of apples (
id:1
) isqt:4
- the quantity of bananas (
id:2
) isqt:2
- the quantity of chocolates (
id:3
) isqt:2
Therefore, by utilizing the cart, the objective is to obtain
orders=[{id:1,qt:4},{id:2, qt:2},{id:3,qt:2}]
. This should clarify things.