开发者

Use JavaScript Variable as Object Identifier

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 Identifi开发者_JAVA技巧er" it should look like this:

{
    "key" : "String",
    "Passed Object Identifier" : {...}
}

What I'm getting is this:

{
    "key" : "String",
    "lcType" : {...}
}

Any ideas?


Javascript 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜