0

I am trying to store cake prices in an object for different sizes for example:

1.5kg = $10 
2kg = $12 
2.5kg = $15 
3kg = $20 

like

let cakePrices = {
   "1.5kg" : "$10",
   "2kg" : "$12",
   "2.5kg" : "$15",
   "3kg" : "$20"
}

I know this goes against variable naming convention since a variable name can't start with a number nor can it have a period(.)
So in what way can I store and retrieve these values?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Tim Mwaura
  • 577
  • 1
  • 7
  • 24
  • 5
    [`cakePrices['2.5kg']`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors) – Yoshi Jun 15 '20 at 07:39
  • Also: https://stackoverflow.com/questions/12953704/how-to-access-object-properties-containing-special-characters – user120242 Jun 15 '20 at 07:42
  • 1
    It is also not recommended to store strings that have to be parsed. Use grammes and assume dollars: `let cakePricesGrammesInDollar = { "1500" : 10, "2000" : 12, "2500" : 15, "3000" : 20 }` – mplungjan Jun 15 '20 at 07:46
  • Thank you @mplungjan I've seen that using "1.5kg" format will cause issues in storing in the database as well – Tim Mwaura Jun 15 '20 at 07:55

0 Answers0