user5508297 user5508297 - 1 year ago 184
Javascript Question

How to set variable key and value in Google Chrome local storage sync?

I'm using Chrome's version of localStorage (

) and I want to use a variable key and value when setting what data to store with it.

I'd like to be able to do something like this:{ user: id }, callback);

where the user and id are dynamically generated.


var user = "Bob";
var id = "81256309";{ user: id }, callback);

doesn't work because it doesn't interpret
as their underlying string values and, as a result, syncs to Chrome with this object:
{ user: id }

I know how to do this with normal JavaScript objects but the method in that thread won't work in this case because I don't have control over the storage.sync object of my Google account.

Answer Source

See Computed property names in ES6, you could use{ [user]: id }, callback);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download