首页 > 解决方案 > 是否可以使用 jquery 变量值作为点符号 javascript 中的键来查找和数组值

问题描述

我试图在点表示法 javascript 中使用 jQuery 变量从多维 JSON 数组中检索值

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

我怀疑这是因为我将一个值作为键或其他东西传入,但我无法完全理解它。或者,当它需要是其他东西时,它可能是一个字符串。

任何帮助,将不胜感激

标签: javascriptjqueryarraysjson

解决方案


推荐阅读