1

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?

dSquared
  • 9,725
  • 5
  • 38
  • 54
  • 1
    possible duplicate of [Passing in dynamic key:value pairs to an object literal?](http://stackoverflow.com/questions/4119324/passing-in-dynamic-keyvalue-pairs-to-an-object-literal) ... this question was already asked often enough ... please use the search before you ask. – Felix Kling Sep 04 '11 at 22:05
  • @Felix Kling Thank you for pointing that out; I'll do a more thorough search next time. – dSquared Sep 04 '11 at 22:15

2 Answers2

9

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' }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3
var o = {"key" : "String"};
o[lcType] = dataObj;
Niko
  • 26,516
  • 9
  • 93
  • 110