如何在Google Chrome本地存储同步中设置变量键和值? [英] How to set variable key and value in Google Chrome local storage sync?
问题描述
我正在使用Chrome版本的localStorage( chrome.storage.sync
),我想设置要存储的数据时,请使用变量键和值.
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);
动态生成用户和ID的位置.
where the user and id are dynamically generated.
此:
var user = "Bob";
var id = "81256309";
chrome.storage.sync.set({ user: id }, callback);
不起作用,因为它不会将user
和id
解释为它们的基础字符串值,因此,该对象与{ user: id }
同步到Chrome.
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 }
.
我知道如何使用普通的JavaScript对象,但是该方法在这种情况下,该线程将无法工作,因为我无法控制我的Google帐户的storage.sync对象.
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.
推荐答案
请参见 ES6中的计算属性名称,您可以使用
chrome.storage.sync.set({ [user]: id }, callback);
这篇关于如何在Google Chrome本地存储同步中设置变量键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!