8

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!');
});
Rob W
  • 341,306
  • 83
  • 791
  • 678
Alex Coroza
  • 1,747
  • 3
  • 23
  • 39
  • is this ``StorageArea.remove(string or array of string keys, function callback)`` the same method as yours? – doniyor Jul 30 '14 at 08:31
  • http://stackoverflow.com/questions/17927378/chrome-storage-sync-remove-array-doesnt-work – Lakshay Dulani Jul 30 '14 at 08:31
  • @doniyor Im not sure but I think thats it. here is the documentation im using [link]https://developer.chrome.com/apps/storage – Alex Coroza Jul 30 '14 at 08:35
  • @AlexCoroza yeah then it is the same – doniyor Jul 30 '14 at 08:35
  • Voting to reopen. This is not a duplicate, @RobW. – Xan Jul 30 '14 at 08:36
  • @Lakshay I saw that solution earlier but it can only remove the whole object. In my case, it will delete the "users" object – Alex Coroza Jul 30 '14 at 08:37
  • @doniyor following the documentation, I can only remove the whole "users". What I want is to remove some items in "users" – Alex Coroza Jul 30 '14 at 08:40
  • @RobW Maybe I'm not fully awake yet, but I don't see how the duplicate deals with deleting sub-items (like this question wants, and you correctly said that it's impossible). It deals with deleting multiple top-level items at once. – Xan Jul 30 '14 at 08:42
  • @Xan It's me who was not awake. I have re-opened the question and converted my comment to an answer. (I'll delete this comment also after you've read it, it is not relevant to future visitors) – Rob W Jul 30 '14 at 08:44
  • @AlexCoroza i see. your question isnot duplicate – doniyor Jul 30 '14 at 09:06

2 Answers2

7

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.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • ah I see. I thought there is a built in function for that. ok i'll use this solution – Alex Coroza Jul 30 '14 at 08:47
  • @AlexCoroza The reason behind not having such a function is that despite automagically accepting and returning JSON-compatible values, `chrome.storage` serializes them, and therefore it's database is made of strings that have no "sub-items". – Xan Jul 30 '14 at 10:16
3

Yes you can try this

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

see documentation

MD SHAYON
  • 7,001
  • 45
  • 38