Javascript chrome.storage.sync.remove阵列不';行不通

Javascript chrome.storage.sync.remove阵列不';行不通,javascript,google-chrome-extension,google-chrome-storage,Javascript,Google Chrome Extension,Google Chrome Storage,我正在做一个小的镀铬扩展。我想使用chrome.storage,但我无法让它从存储中删除多个项目(数组)。单项拆卸工程 function clearNotes(symbol) { var toRemove = "{"; chrome.storage.sync.get(function(Items) { $.each(Items, function(index, value) { toRemove += "'" + index + "',"

我正在做一个小的镀铬扩展。我想使用
chrome.storage
,但我无法让它从存储中删除多个项目(数组)。单项拆卸工程

function clearNotes(symbol)
{
    var toRemove = "{";

    chrome.storage.sync.get(function(Items) {
        $.each(Items, function(index, value) {
            toRemove += "'" + index + "',";         
        });
        if (toRemove.charAt(toRemove.length - 1) == ",") {
            toRemove = toRemove.slice(0,- 1);
        }
        toRemove = "}";
        alert(toRemove);
    });

    chrome.storage.sync.remove(toRemove, function(Items) {
        alert("removed");
        chrome.storage.sync.get( function(Items) {
            $.each(Items, function(index, value) {
                alert(index);           
            });
        });
    });
}; 

似乎没有任何东西中断,但最后一个提醒存储中的内容的循环仍然显示我试图删除的所有值。

当您将字符串传递到
sync.remove
时,Chrome将尝试删除键与输入字符串匹配的单个项。如果需要删除多个项,请使用键值数组

此外,您应该将
remove
调用移动到
get
回调中

function clearNotes(symbol)
{
// CHANGE: array, not a string
var toRemove = [];

chrome.storage.sync.get( function(Items) {
    $.each(Items, function(index, value)
    {
        // CHANGE: add key to array
        toRemove.push(index);         
    });

    alert(toRemove);

    // CHANGE: now inside callback
    chrome.storage.sync.remove(toRemove, function(Items) {
        alert("removed");

        chrome.storage.sync.get( function(Items) {
            $.each(Items, function(index, value)
            {
                alert(index);           
            });
        });
    }); 
});

}; 

就这样!非常感谢。将其作为答案提交,以便我将其标记为解决方案。