Home > Software engineering >  Add same object to an array
Add same object to an array

Time:01-30

I've created a shopping cart, and right now the the cart contains a single apple, a single orange, and the total prices.

How would I add a second "apple" without creating another object?

For example, I now would like to add 2 apples to the cart, so the price in the array gets updated, as well as the total amount.

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

CodePudding user response:

You may try to add the quantity also. Please try the following code:

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

}

CodePudding user response:

You can try this code:

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;

CodePudding user response:

this now adds the total amount in a separate variable

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

  •  Tags:  
  • Related