admin管理员组文章数量:1290974
I am trying to store the following object in the browser local store via a jQuery plugin (Lawnchair):
{"key" : lcName, lcType : dataObj}
The problem I'm having is that 'lcType' is a variable (of type string) passed to the function which stores the above object, however it is not being used as the object identifier, instead the string "lcType" ends up being used.
If lcType = "Passed Object Identifier" it should look like this:
{
"key" : "String",
"Passed Object Identifier" : {...}
}
What I'm getting is this:
{
"key" : "String",
"lcType" : {...}
}
Any ideas?
I am trying to store the following object in the browser local store via a jQuery plugin (Lawnchair):
{"key" : lcName, lcType : dataObj}
The problem I'm having is that 'lcType' is a variable (of type string) passed to the function which stores the above object, however it is not being used as the object identifier, instead the string "lcType" ends up being used.
If lcType = "Passed Object Identifier" it should look like this:
{
"key" : "String",
"Passed Object Identifier" : {...}
}
What I'm getting is this:
{
"key" : "String",
"lcType" : {...}
}
Any ideas?
Share Improve this question asked Sep 4, 2011 at 21:58 dSquareddSquared 9,8355 gold badges39 silver badges54 bronze badges 2- 1 possible duplicate of Passing in dynamic key:value pairs to an object literal? ... this question was already asked often enough ... please use the search before you ask. – Felix Kling Commented Sep 4, 2011 at 22:05
- @Felix Kling Thank you for pointing that out; I'll do a more thorough search next time. – dSquared Commented Sep 4, 2011 at 22:15
2 Answers
Reset to default 9Javascript objects are just associative arrays, so you can treat them as such:
var foo = { 'key' : 'some key' };
var lcType = 'foo';
foo[lcType] = 'bar';
// foo now looks like this { 'key' : 'some key', 'foo': 'bar' }
var o = {"key" : "String"};
o[lcType] = dataObj;
本文标签: jqueryUse JavaScript Variable as Object IdentifierStack Overflow
版权声明:本文标题:jquery - Use JavaScript Variable as Object Identifier - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741505635a2382302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论