如何更新嵌套对象中的LocalStorage中的某个值?

时间:2018-06-14 15:44:59

标签: google-chrome-extension local-storage

我这样设置:

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

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

1 个答案:

答案 0 :(得分: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});
    });