Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda