views:

60

answers:

4

Hey there,

I am using the following method to basically create a JSON string.

var saveData = {};
saveData.a = 2;
saveData.c = 1;

However the .a and .c don't cut it for what I need to do, I need to replace these with strings. So something like..

var name = 'wibble';
saveData.name = 2;

This would get accessed with

saveData.wibble

Does anyone know how this could be achieved?

+2  A: 

Use the map accessor:

var name = 'wibble'
saveData[name] = 2
tvanfosson
+3  A: 
var name = "wibble";
saveData[name] = 2;

alert(saveData.wibble);

Note that, in JavaScript, the following notations are equivalent:

obj.key
obj["key"]
Ferdinand Beyer
+1  A: 

You can access Javascript objects using a dictionary notation:

var name = 'wibble';
saveData[name] = 2;

saveData.wibble is now 2.

Kai
A: 

i want to access an opject or function useing variable how can i do that?

mohammad
Hey Mohammad,You will get a better response by creating a fresh question.
Toby