Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!