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

两盒软妹~` 提交于 2020-04-30 06:23:47

问题


I'm using Chrome's version of localStorage (chrome.storage.sync) 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:

chrome.storage.sync.set({ user: id }, callback);

where the user and id are dynamically generated.

This:

var user = "Bob";
var id = "81256309";

chrome.storage.sync.set({ user: id }, callback);

doesn't work because it doesn't interpret user and id 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.


回答1:


See Computed property names in ES6, you could use

chrome.storage.sync.set({ [user]: id }, callback);


来源:https://stackoverflow.com/questions/38779855/how-to-set-variable-key-and-value-in-google-chrome-local-storage-sync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!