[英]Accessing a Dynamically Named Object Property
I'm creating a javavascript object which I'm calling rulesObject. 我正在创建一个javavascript对象,我将其称为rulesObject。 The idea is for it to be a javascript object containing all of the rules I need to check to enable/disable other checkboxes that is dynamically generated from a mysql database at the very beginning of the script.
这个想法是让它成为一个javascript对象,其中包含我需要检查以启用/禁用其他复选框的所有规则,这些复选框是在脚本开始时从mysql数据库动态生成的。 For now, I'm just testing it out with two rules which I know create the scenario I'm looking for, so here's what my object looks like at the moment:
现在,我只是用两个规则进行测试,我知道这两个规则创建了我要寻找的场景,因此,这是当前对象的外观:
rulesObject = {
chk533570 : ["533577", "503671", "503667", "604028", "503661"],
chk503928 : ["533577", "533578","503671", "503666", "533576", "503667", "324201", "503221", "604028", "503668", "533580", "503669", "533579", "533581", "503670"]
};
Now what I need to do is access the information out of that object. 现在,我需要做的是从该对象访问信息。 If I do a simple alert(rulesObject. chk533570), it works PERFECTLY – gives me exactly what I need.
如果我执行一个简单的警报(rulesObject。chk533570),则它可以正常工作-完全满足我的需求。 However, what I'm going to need to do is access a specific rule based on what was just clicked by running through the following.
但是,我需要做的是根据通过浏览以下内容单击的内容来访问特定规则。 So, for example, if I clicked the checkbox valued "533570", it would go through the following:
因此,例如,如果我单击值为“ 533570”的复选框,它将经历以下过程:
$('input').click(function(){
if(this.checked) {
checkRules(this.value, 'checked');
} else {
checkRules(this.value, 'unchecked');
}
});
(Of course I'm using jQuery there, but I'm using it throughout the web app so I don't mind going back and forth.) (当然,我在那里使用jQuery,但是我在整个Web应用程序中都使用了jQuery,所以我不介意来回走动。)
Now onto my checkRules function. 现在到我的checkRules函数。 It's still very simple as it's in the beginning stages – I just want to alert the value of what I just selected.
在开始阶段,它仍然非常简单–我只想提醒我刚刚选择的内容的价值。 Again, if I do alert(rulesObject. chk533570), even within the function, I get the right result, but I need to access what I just selected, so I have to add the letters 'chk' to the beginning of the object property name and then append the justselected value (which in this case equals 533570).
同样,如果我执行了alert(rulesObject。chk533570),即使在函数中,我也会得到正确的结果,但是我需要访问刚刚选择的内容,因此必须在对象属性的开头添加字母“ chk”命名,然后附加刚才选择的值(在这种情况下等于533570)。 Here are the ways I've tried to do it:
这是我尝试执行的方法:
function checkRules(justselected, state) {
rulename= 'chk' + justselected;
currentrules = rulesObject.rulename;
alert(rulename);
alert(currentrules);
}
Alert 1: chk533570 Alert 2: undefined 警报1:chk533570警报2:未定义
function checkRules(justselected, state) {
rulename= 'chk' + justselected;
alert(rulesObject.rulename);
}
Alert: Undefined 警报:未定义
function checkRules(justselected, state) {
rulename= 'chk' + justselected;
alert(rulesObject + '.chk' + justselected);
}
Alert: [object Object].chk533570 警告:[object Object] .chk533570
function checkRules(justselected, state) {
alert(rulesObject.chk533570);
}
Alert: 533577,503671,503667,604028,503661 警报:533577,503671,503667,604028,503661
So, any idea how to properly call that name so that I get the right results? 那么,有什么主意如何正确地命名该名称,以便获得正确的结果? I also tried not having the 'chk' in there at all, but the javascript object didn't like a completely numeral property.
我也尝试过根本没有'chk',但是javascript对象不喜欢完全数字的属性。
obj.key
is the same as obj['key']
- but in the second way the key can be dynamic since it's a plain JavaScript expression. obj.key
与obj['key']
-但是在第二种方式中,键可以是动态的,因为它是纯JavaScript表达式。
So you can simply use rulesObject['chk' + justselected]
: 因此,您可以简单地使用
rulesObject['chk' + justselected]
:
function checkRules(justselected, state) {
alert(rulesObject['chk' + justselected]);
}
Long time ago people used to use
alert(eval('rulesObject.chk' + justselected));
很久以前人们就习惯使用
by the way. alert(eval('rulesObject.chk' + justselected));
顺便说说。 While this works, do not use this .
在此有效的同时, 请勿使用this 。 Using
eval()
should be avoided at all times; 应始终避免使用
eval()
; and in this case there is a much cleaner way anyway. 在这种情况下,总有一种更清洁的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.