0

Im trying to use a jQuery variable in dot notation javascript to retrieve a value from a multidimensional JSON array

var products = {
  "p1": {
    "price":"12.98"
  },
  "p2": {
    "price":"9.98"
  }
}

var pid = $(this).attr('id');
//console returns p1

var price = products.pid.price;
console.log(price);
//console returns Uncaught TypeError: Cannot read property 'price' of undefined

I suspect this is due to me passing a value in as a key or something but can't get my head around it all. Or it could be a string when it needs to be something else.

Any help would be appreciated

Aparistar
  • 185
  • 1
  • 5
  • 15

0 Answers0