如何将多个值推送到chrome.storage.local数组键? [英] How to push multiple values to a chrome.storage.local array key?

查看:183
本文介绍了如何将多个值推送到chrome.storage.local数组键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码为本地存储设置key:值:

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



我可以做些什么来为键键添加多个值?

解决方案首先使用 get 方法,然后使用 get 回调中的 set 将新存储数据作为键/值对添加到从 GET 。例子:

  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);
});


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"?

解决方案

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); 
});

这篇关于如何将多个值推送到chrome.storage.local数组键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆