admin管理员组文章数量:1410705
If I have a bunch of objects, and within these objects is the string "id" (which is the same as the object name) how can I use this string to reference the object?
Example:
//These objects are tests - note the id's are the same as the object name
var test1 = {
id : "test1",
data : "Test 1 test 1 test 1"
}
var test2 = {
id : "test2",
data : "Test 2 test 2 test 2"
}
/* ----- My Function ----- */
var myObj = null;
function setMyObj(obj){
myObj = obj;
}
setMyObj(test1);
/* ----- My Function ----- */
Now, if I call the following:
myObj.id;
The Result is "test1" (a string). If I want to use this to get the data from test1, how would I do so?
"myObj.id".data
[myObj.id].data
^^^
These don't work!
Cheers, Rich
If I have a bunch of objects, and within these objects is the string "id" (which is the same as the object name) how can I use this string to reference the object?
Example:
//These objects are tests - note the id's are the same as the object name
var test1 = {
id : "test1",
data : "Test 1 test 1 test 1"
}
var test2 = {
id : "test2",
data : "Test 2 test 2 test 2"
}
/* ----- My Function ----- */
var myObj = null;
function setMyObj(obj){
myObj = obj;
}
setMyObj(test1);
/* ----- My Function ----- */
Now, if I call the following:
myObj.id;
The Result is "test1" (a string). If I want to use this to get the data from test1, how would I do so?
"myObj.id".data
[myObj.id].data
^^^
These don't work!
Cheers, Rich
Share Improve this question edited Jun 19, 2012 at 14:01 Sarfraz 383k82 gold badges559 silver badges612 bronze badges asked Jun 19, 2012 at 13:59 RichardRichard 1,1584 gold badges18 silver badges33 bronze badges 2-
1
myObj.data
should do the job. – Furqan Hameedi Commented Jun 19, 2012 at 14:04 - Hey Furqan - that get's me myObj data - I want test1's data (which could have changed in the meantime)! – Richard Commented Jun 19, 2012 at 14:13
4 Answers
Reset to default 4If your variables are defined on a global scope, the following works
window[ myObj.id ].data
If you are in a function's scope, things get a lot harder. Easiest way then is to define your objects in a specific namespace on window and retrieve the objects similar to the above code.
Store test1 and test2 in a key-value collection (aka an object.) Then access it like:
collection[myObj.id].data
If you want to refer to something using a variable, then make that something an object property, not a variable. If they are sufficiently related to be accessed in that way, then they are sufficiently related to have a proper data structure to express that relationship.
var data = {
test1: {
id: "test1",
data: "Test 1 test 1 test 1"
},
test2: {
id: "test2",
data: "Test 2 test 2 test 2"
}
};
Then you can access:
alert( data[myObj.id] );
Great answers all, thanks for the help, in case any one finds this in future, this is how I'm going to use it:
var parent = {}
parent.test1 = {
id : "test1",
data : "Test 1 test 1 test 1"
}
parent.test2 = {
id : "test2",
data : "Test 2 test 2 test 2"
}
var myObj = null;
function setMyObj(obj){
myObj = obj;
}
setMyObj(parent.test1);
parent[myObj.id] = null;
//Test1 object is now null, not myObj!
本文标签: Javascriptuse string as object referenceStack Overflow
版权声明:本文标题:Javascript - use string as object reference - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744981859a2635863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论