I cannot add an item to an array

Asked

Viewed 46 times

-1

I am unable to use the ADDITIONAL function well I’m trying to make a cap that person can add the additional in the items however I’m not getting. I am new in the area of programming and I am developing systems for studies.

module.exports = function Cart(cart) {
this.items = cart.items || {};
this.totalItems = cart.totalItems || 0;
this.totalPrice = cart.totalPrice || 0;

this.add = function(item, id) {
    var cartItem = this.items[id];
    if (!cartItem) {
        cartItem = this.items[id] = {item: item, quantity: 0, price: 0, add: {adicional: null, quantidade: 0, preco:0}};
    }
    cartItem.quantity++;
    cartItem.price = cartItem.item.price * cartItem.quantity;
    this.totalItems++;
    this.totalPrice += cartItem.item.price;
};

this.adicional = function(item, id){
    this.items[id].item.add.adicional = item;
};

this.remove = function(id) {
    this.totalItems -= this.items[id].quantity;
    this.totalPrice -= this.items[id].price;
    delete this.items[id];
};

this.getItems = function() {
    var arr = [];
    for (var id in this.items) {
        arr.push(this.items[id]);
    }
    return arr;
};

};

  • 1

    And cart.items would be what?

  • that line this.items[id].item.add.adicional = item; It’s really weird, it wouldn’t be something like this.items[id] = item; or this.items[id].adicional = item; ?

1 answer

0

Some considerations, stop this case have no return, so your additional function does not need to be returned to the variable this.additional, can declare the funciotion normally. In this case it may be returning null because of this or the way to insert, I prefer to use the push for arrays, use within the additional function the following line:

this.items.push(item);

remove the this if return error below:

    Cannot read property 'push' of undefined.

About your code, there are some errors Parenting, I did not see the call of the additional function with the passing of parameter 'item'.

In javascript the best way to insert an item in the array is with push.

At the beginning of the code if Cart.items is null it creates an object {}, I suggest creating an array and placing several objects within that array. Here is an example. To run just create a server.js file and call that file with Node + path + filename. Ex: Node "e: test src server.js".

Follow the example code:

 const items = [];
 item = {item: 1, quantidade: 1}
 adicional(item);

 item = {item: 2, quantidade: 2}
 adicional(item);

 item = {item: 3, quantidade: 3}
 adicional(item);

 function adicional (item) {
      items.push(item);
 }

 console.log(items);
 // resultado
 // [
 //     { item: 1, quantidade: 1 },
 //     { item: 2, quantidade: 2 },
 //     { item: 3, quantidade: 3 }
 //   ]
  • I need to put the additional, in Hamburg in the burger id, each burger will have an additional , but I have not found a solution yet...

  • So Items is the hamburger and item are the things that go in the hamburger? Would that be it? If yes you want to add the extra items in the hamburger from a specific id. In this case Items is an array with several hamburgers and each one can have additional or not? I think it’s nice that you edit your question and put the jsons so we understand better. Without the models it complicates a little, but basically what I told you is the answer. You got to test my code?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.