chrome同步存储以存储和更新阵列 [英] chrome sync storage to store and update array

查看:77
本文介绍了chrome同步存储以存储和更新阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将阵列存储在chrome存储同步中并检索它们?

is it possible to store array in chrome storage sync and retrieve them ?

var uarray = [abc,def,ghi];

是否可以更新存储中的存储阵列?

Is it possible to update the stored array in the storage ?

var tobeadded = jkl;
uarray.push(tobeadded);

这是文档中的语法

chrome.storage.sync.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

我的书签扩展名,需要存储书签的ID,并对其进行检索以用于内部搜索和基于该ID的内容.书签需要定期更新存储同步中的ID.

My bookmark extension, need to store the id of bookmark and retrieve them for internal search and stuffs based on it. bookmarking needs update of ID in storage sync periodically.

谢谢!

推荐答案

您可以读取现有值,追加新值并存储回来.

You can read the existing values, append the new value and store back.

下面的示例代码应允许您将newArrEntry添加到存储在chrome.storage.sync

Following sample code should allow you to add newArrEntry into existing array stored in chrome.storage.sync

chrome.storage.sync.get(["storagekey"], function(result) {
        var array = result[storagekey]?result[storagekey]:[];

        array.unshift(newArrEntry);

        var jsonObj = {};
        jsonObj[storagekey] = array;
        chrome.storage.sync.set(jsonObj, function() {
            console.log("Saved a new array item");
        });
    });

这篇关于chrome同步存储以存储和更新阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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