0

I have a class itemCollection that stores information about purchases. This class has array _items as property where purchases are stores. When user adds new purchase in cart this class using addItem method that adds that item in _items property if its already has this item this method iterates quantity property if not adds new item in array.

Problem is that instead of adding new item in array when other item is chosen its keeps incrementing quantity property of a first item that was added.

cartCollection class (object):

var cartCollection = {
    _items: [],
    addItem: function(obj) {
      'use strict';
      var purchase = {
        item: {
          id: obj.id,
          name: obj.name,
          price: obj.price
        },
        thisItemTotal: obj.price,
        quantity: 1
      };
      var result = _.findWhere(this._items, purchase.item.id);
      console.log(result);
      if (typeof result != 'undefined') {
        //console.log(result);
        var index = _.findIndex(this._items, {
          id: result.item.id
        });
        //console.log(index);
        result.quantity++;
        this._items[index] = result;
        this._itemTotalPrice();
      } else if (typeof result === 'undefined') {
        console.log("Im was called!");
        this._items.push(purchase);
        console.log(this._items);
      }
    },
    ...
1
  • I know, this is habit after other languages Commented Jun 2, 2015 at 20:21

2 Answers 2

1

Since purchase doesn't have an ID, but has an "item" with an ID, The correct find statement should be:

var result = _.find(this._items, function(item) {
   return item.item.id == purchase.item.id;
});

It might be better to rename _items to _purchases in order to disambiguate

The complete code should be something like:

addItem: function(obj) {
  'use strict';
  var purchase = {
    item: _.pick(obj, 'id', 'name', 'price')
    thisItemTotal: obj.price,
    quantity: 1
  };

  var result = _.find(this._items, function(item) {
    return item.item.id == purchase.item.id;
  });

  console.log(result);

  if (result) {
    result.quantity++;
    this._itemTotalPrice();
  }
  else {
    console.log("Im was called!");
    this._items.push(purchase);
    console.log(this._items);
  }
},
Sign up to request clarification or add additional context in comments.

Comments

0

Your findWhere statement is broken. It should be:

var result = _.findWhere(this._items, {id:purchase.item.id});

Good luck

4 Comments

Now it adds new object but not increments quantity.
you're pushing purchase instead of purchase.item
Did I understood you correctly I'm need to use it like this: this._items.push(purchase.item);? If that so now im getting: Uncaught TypeError: Cannot read property 'id' of undefined
it's pretty impossible to answer your questions without all the context. I am going to leave it at this and wish you good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.