0

I want to make a comparison of an object literal which looks like so...

receipt.tab = {Cafe Latte: 4.75, Cappucino: 3.85}

The items are added when I call the method addItemAndPrice(item) as found bellow...

var Receipt = function(){
    this.tab = {};
};

Receipt.prototype.addItemAndPrice = function(item){
    if (comparisonHere???){
        this.tab[item] = this.tab[item] + this.tab[item] = menu.prices[item];
    } else {
        this.tab[item] = menu.prices[item];
    }
};

I want to call the method and if there is already a Cafe Latte found within the tab then I want to add the value of that item to the corresponding item value.

and create this...

receipt.tab = {Cafe Latte: 9.50, Cappucino: 3.85}

FYI menu looks like this...

var menu = {
  "shopName": "The Coffee Connection",
  "address": "123 Lakeside Way",
  "phone": "16503600708",
  "prices": 
  {
  "Cafe Latte": 4.75,
  "Flat White": 4.75,
  "Cappucino": 3.85,
  "Single Espresso": 2.05,
  "Double Espresso": 3.75,
  "Americano": 3.75,
  "Cortado": 4.55,
  "Tea": 3.65,
  "Choc Mudcake": 6.40,
  "Choc Mousse": 8.20,
  "Affogato": 14.80,
  "Tiramisu": 11.40,
  "Blueberry Muffin": 4.05,
  "Chocolate Chip Muffin": 4.05,
  "Muffin Of The Day": 4.55
  }
}
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
Kieran Goodacre
  • 147
  • 1
  • 2
  • 7
  • Part of your question already has an answer -- [how do i check if an object has a key in javascript?](http://stackoverflow.com/questions/455338/how-do-i-check-if-an-object-has-a-key-in-javascript) – Jonathan Lonowski Mar 19 '15 at 17:49
  • Is this intentional? `this.tab[item] = this.tab[item] + this.tab[item] = menu.prices[item];` Notice the two assignments `=` happening there. – bloodyKnuckles Mar 19 '15 at 17:49
  • In addition to following @alecmce's advice and checking out the question linked above, I would like to make the suggestion to clean up your data model if you have any control over it. I would store the amount separately per item instead of increasing the price for starters. – PermaFrost Mar 19 '15 at 17:52

2 Answers2

0

if (receipt.tab.hasOwnProperty(item)) {...} is best, because it catches cases where receipt.tab[item] is defined but falsy (like {'Caffe Latte': 0}), but in your case, if (receipt.tab[item]) {...} probably works because all falsy values should resolve to 0 anyway.

alecmce
  • 1,456
  • 10
  • 23
0

You can use the hasOwnProperty method to check if a property exists. If it does, you would just change the value:

Receipt.prototype.addItemAndPrice = function(item){
    if (this.tab.hasOwnProperty(item)){
        this.tab[item] += menu.prices[item];
    } else {
        this.tab[item] = menu.prices[item];
    }
};

You can simplify this if you know that a valid value can never be false, or if that value can be considered zero. Then you can just replace any falsy value with zero and assign the sum:

Receipt.prototype.addItemAndPrice = function(item){
    this.tab[item] = (this.tab[item] || 0) + menu.prices[item];
};
Guffa
  • 687,336
  • 108
  • 737
  • 1,005