Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.storage remove specific item from an array

This is the JSON stored in my chrome local storage

{"users":[
    {"password":"123","userName":"alex"},
    {"password":"234","userName":"dena"},
    {"password":"343","userName":"jovit"}
]}

Is it possible to remove a specific item in "users" ? I tried to this code but no luck

chrome.storage.local.remove('users[0]', function(){
    alert('Item deleted!');
});
like image 885
boi_echos Avatar asked Jul 30 '14 08:07

boi_echos


People also ask

Can content scripts access Chrome storage?

User data can be automatically synced with Chrome sync (using storage. sync ). Your extension's content scripts can directly access user data without the need for a background page.

Do Chrome extensions take up storage?

Even if you're not actively using them, every open tab and enabled extension consumes disk space. It is easy to examine Chrome tabs and extensions that might be consuming too many resources in Chrome Task Manager.

Does Chrome sync local storage?

sync , the stored data will automatically be synced to any Chrome browser that the user is logged into, provided the user has sync enabled. When Chrome is offline, Chrome stores the data locally. The next time the browser is online, Chrome syncs the data. Even if a user disables syncing, storage.


2 Answers

There is no magic syntax to delete only one element from an array that is stored in chrome.storage. In order to delete an item from the array, you has to retrieve the stored array, throw away the unwanted items (or equivalently, keep only the items that you want to keep), then save the array again:

chrome.storage.local.get({users: []}, function(items) {
    // Remove one item at index 0
    items.users.splice(0, 1);
    chrome.storage.set(items, function() {
        alert('Item deleted!');
    });
});

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice.

Note that if you want to delete one or more items whose value satisfies a certain condition, you have to walk the array in reverse order. Otherwise you may end up removing the wrong items since the indices of the later elements are off by one after removing the first item, off by two when you've removed two items, etc.

like image 90
Rob W Avatar answered Oct 13 '22 02:10

Rob W


Yes you can try this

chrome.storage.sync.remove("token");

see documentation

like image 28
MD SHAYON Avatar answered Oct 13 '22 02:10

MD SHAYON