How to push multiple values to a chrome.storage.local array key?

核能气质少年 提交于 2019-12-12 14:24:13

问题


I'm using the following code to set a key:value to the local storage:

chrome.storage.local.set({"key": value}, null);


What can I do to add multiple values to the key "key"?


回答1:


First use the get method and then use set inside the get callback to add your new storage data as a key/value pair to the storage object returned from get. Example:

chrome.storage.local.get(function(cfg) {
  if(typeof(cfg["key"]) !== 'undefined' && cfg["key"] instanceof Array) { 
    cfg["key"].push("value");
  } else {
    cfg["key"] = ["value"];
  }
  chrome.storage.local.set(cfg); 
});


来源:https://stackoverflow.com/questions/23270282/how-to-push-multiple-values-to-a-chrome-storage-local-array-key

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