JavaScript“关联”数组访问

发布于 2024-08-26 21:21:41 字数 387 浏览 3 评论 0原文

我有一个包含两个元素的简单模拟数组:

bowl["fruit"] = "apple";
bowl["nuts"] = "brazilian";

我可以通过如下事件访问该值:

onclick = "testButton00_('fruit')">with `testButton00_`

function testButton00_(key){
    var t = bowl[key];
    alert("testButton00_: value = "+t);
}

但是,每当我尝试使用只是非显式字符串的键从代码内访问该数组时,我都会得到 未定义。。我是否必须以某种方式传递带有转义“密钥”的参数?

I have a simple simulated array with two elements:

bowl["fruit"] = "apple";
bowl["nuts"] = "brazilian";

I can access the value with an event like this:

onclick = "testButton00_('fruit')">with `testButton00_`

function testButton00_(key){
    var t = bowl[key];
    alert("testButton00_: value = "+t);
}

However, whenever I try to access the array from within code with a key that is just a non-explicit string, I get undefined. Do I have somehow have to pass the parameter with the escaped 'key'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柏拉图鍀咏恒 2024-09-02 21:21:41

键可以是动态计算的字符串。举一个你通过但不起作用的例子。

给定:

var bowl = {}; // empty object

您可以说:

bowl["fruit"] = "apple";

或:

bowl.fruit = "apple"; // NB. `fruit` is not a string variable here

甚至:

var fruit = "fruit";
bowl[fruit] = "apple"; // now it is a string variable! Note the [ ]

或者如果您确实想要这样做:

bowl["f" + "r" + "u" + "i" + "t"] = "apple";

这些对 bowl 对象都有相同的效果。然后您可以使用相应的模式来检索值:

var value = bowl["fruit"];
var value = bowl.fruit; // fruit is a hard-coded property name
var value = bowl[fruit]; // fruit must be a variable containing the string "fruit"
var value = bowl["f" + "r" + "u" + "i" + "t"];

The key can be a dynamically computed string. Give an example of something you pass that doesn't work.

Given:

var bowl = {}; // empty object

You can say:

bowl["fruit"] = "apple";

Or:

bowl.fruit = "apple"; // NB. `fruit` is not a string variable here

Or even:

var fruit = "fruit";
bowl[fruit] = "apple"; // now it is a string variable! Note the [ ]

Or if you really want to:

bowl["f" + "r" + "u" + "i" + "t"] = "apple";

Those all have the same effect on the bowl object. And then you can use the corresponding patterns to retrieve values:

var value = bowl["fruit"];
var value = bowl.fruit; // fruit is a hard-coded property name
var value = bowl[fruit]; // fruit must be a variable containing the string "fruit"
var value = bowl["f" + "r" + "u" + "i" + "t"];
晨曦÷微暖 2024-09-02 21:21:41

如果您不想转义密钥,可以使用 JSON:

var bowl = {
  fruit: "apple",
  nuts: "brazil"
};

alert(bowl.fruit);

You could use JSON if you don’t want to escape the key:

var bowl = {
  fruit: "apple",
  nuts: "brazil"
};

alert(bowl.fruit);
兮颜 2024-09-02 21:21:41

我不确定我是否理解你。您可以确保密钥是这样的字符串

if(!key) {
  return;
}
var k = String(key);
var t = bowl[k];

或者您可以检查密钥是否存在:

if(typeof(bowl[key]) !== 'undefined') {
  var t = bowk[key];
}

但是,我认为您没有发布无效代码?

I am not sure I understand you. You can make sure the key is a string like this

if(!key) {
  return;
}
var k = String(key);
var t = bowl[k];

Or you can check if the key exists:

if(typeof(bowl[key]) !== 'undefined') {
  var t = bowk[key];
}

However, I don't think you have posted the non-working code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文