Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
Agregar el mismo objeto a una matriz

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); }, };
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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

}

about 4 years ago · Juan Pablo Isaza Denunciar

0

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); } }
about 4 years ago · Juan Pablo Isaza Denunciar

0

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;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda