Syam Kumar S Syam Kumar S - 1 year ago 108
Javascript Question

Using a variable key in

I am creating a chrome-extension.
I don't know how to use a variable as the key to
function. I have tried

var key = 'myKey';, function(val) {{key:val[key]+param1}); //appending param1

Here i am trying to get the value of
and append a string
and place it back to the storage using the same key. But I am not able to do so.
the alert box displays undefined all the time.

But when I try without the variable
, it works fine.'myKey', function(val) {{myKey:val['myKey']+param1}); //appending param1

The problem is that I am using
can have arguments of not only string but also objects.

Answer Source

I have solved the problem. Instead of using{key:val[key]+param1});

I have replaced the json string {key:val[key]+param1} with an object.

var obj = {};       
var key = "myKey";  
obj[key] += param1;;

Here obj has value {"myKey":"the appended value"}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download