Creé un carrito de compras, y ahora mismo el carrito contiene una sola manzana, una sola naranja y los precios totales.
¿Cómo agregaría una segunda "manzana" sin crear otro objeto?
Por ejemplo, ahora me gustaría agregar 2 manzanas al carrito, para que se actualice el precio en la matriz, así como la cantidad total.
module.exports = { shoppingCart: function () { //create empty cart array theCart = []; // create two seperate versions of the same kind of object using class method class Fruits { constructor(fruit, price) { this.fruit = fruit; this.price = price; // this.quantity = price * 2; } } let fruit1 = new Fruits('Apple', 4.95); // create new object. sets the value of this let fruit2 = new Fruits('Orange', 3.99); let fruit3 = new Fruits('Total amount', fruit1.price + fruit2.price); // combine both fruits into an array let bothFruits = [fruit1, fruit2, fruit3]; //add items to the cart Array.prototype.push.apply(theCart, bothFruits); //remove items from the cart by calling this function function removeAllItems() { if ((theCart.length = !0)) { theCart = []; } } //removeAllItems(); console.log(theCart); }, };También puede intentar agregar la cantidad. Por favor, intente con el siguiente código:
module.exports = { shoppingCart: function () { //create empty cart array theCart = []; // create two seperate versions of the same kind of object using class method class Fruits { constructor(fruit, price, quantity) { this.fruit = fruit; this.quantity = quantity; this.price = price * quantity; } } let fruit1 = new Fruits("Apple", 4.95, 2); // create new object. sets the value of this let fruit2 = new Fruits("Orange", 3.99, 1); let fruit3 = new Fruits("Total amount", fruit1.price + fruit2.price); // combine both fruits into an array let bothFruits = [fruit1, fruit2, fruit3]; //add items to the cart Array.prototype.push.apply(theCart, bothFruits); //remove items from the cart by calling this function function removeAllItems() { if (theCart.length = !0) { theCart = []; } } //removeAllItems(); console.log(theCart); }}
esto ahora agrega la cantidad total en una variable separada
module.exports = { shoppingCart: function () { //create empty cart array theCart = []; // create two seperate versions of the same kind of object using class method class Fruits { constructor(fruit, price, quantity) { this.fruit = fruit; this.quantity = quantity; this.price = price * quantity; } } let fruit1 = new Fruits("Apple", 4.95, 2); // create new object. sets the value of this let fruit2 = new Fruits("Orange", 3.99, 1); //let fruit3 = new Fruits("Total amount", fruit1.price * fruit2.price); // combine both fruits into an array let bothFruits = [fruit1, fruit2]; //add items to the cart Array.prototype.push.apply(theCart, bothFruits); let total = fruit1.price + fruit2.price; //remove items from the cart by calling this function function removeAllItems() { if (theCart.length = !0) { theCart = []; } } //removeAllItems(); console.log(theCart, total); } }Puedes probar este código:
class Fruits { constructor() { this.items = []; this.totals = 0; } calculateTotals() { this.totals = 0; this.items.forEach(item => { let price = item.price; let quantity = item.quantity; let amount = price * quantity; this.totals += amount; }); } addToCart(fruit, price, quantity) { let obj = { fruit, price, quantity } this.items.push(obj); this.calculateTotals(); } get cart() { return { items: this.items, totals: this.totals } } } const fruitsCart = new Fruits(); fruitsCart.addToCart("Apple", 10.5, 2); fruitsCart.addToCart("Orang", 15, 1); const cart = fruitsCart.cart;