如何使用 Chrome 存储空间从对象中移除数据

how to remove data from a object using chrome storage?

本文关键字:数据 对象 何使用 Chrome 存储空间      更新时间:2023-09-26

我有一个对象存储在Chrome存储中,如下所示:

{
    "planA": 
    {
        123: {key: 'some key'}
        124: {key: 'some other key'}
    },
    "planB": 
    {
        223: {key: 'some key'}
        234: {key: 'some other key'}
    }
}

我想做一些类似chrome.storage.sync.remove([{"planA": "123"}]);

但这似乎行不通Error in response to storage.get: Error: Invalid value for argument 1. Value does not match any valid type choices.

从文档StorageArea.remove(string or array of string keys, function callback)

蚂蚁的想法?

您无法在单个 API 调用中执行此操作。API 仅提供对顶级密钥的访问权限:您可以获取或设置整个planA

因此,您需要编写自己的 get/set 函数,用于检索所需的顶级键,根据需要修改对象,然后将其保存回来。