【问题标题】:How do I update a certain value in LocalStorage inside a nested object?如何更新嵌套对象内 LocalStorage 中的某个值?
【发布时间】:2022-03-28 18:04:25
【问题描述】:

我是这样设置的:

chrome.storage.local.set({
    myState: {
        data: {
            website: "google.com",
            visited: false,
            count: 0
        }
    }
})

如果我想将 myState.data.visited 更改为 true,我该怎么办?

【问题讨论】:

  • 先读,改对象,再写结果。没有其他办法。

标签: javascript google-chrome-extension local-storage


【解决方案1】:

您需要获取数据,对其进行更改并再次保存。

    chrome.storage.local.get('storageKey', function(result) {
      //data.visited will be in the result object for a specific key. You can change data.visited 
      //to be true here. After changing it to true you can save it again under 
      //the key 'storageKey' or any key you like.

      chrome.storage.local.set({storageKey: result});
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2015-06-25
    • 2017-09-22
    • 1970-01-01
    • 2017-02-27
    • 2021-05-22
    • 2016-08-26
    相关资源
    最近更新 更多