2👍
✅
You can only use localStorage.removeItem()
to remove entire localStorage
entries. What you’re trying to do is remove part of a localStorage
entry. The only way to do this is to retrieve the object, remove the key like you would a normal JS object, and then re-set the localStorage
entry:
removeAccount(state, account){
const accounts = JSON.parse(localStorage.getItem('accounts'));
delete accounts[account.apikey];
localStorage.setItem("accounts", JSON.stringify(accounts));
}
👤izb
2👍
Data in localStorage is kept as a string. So you need to read the entire string, remove the item, then write it back:
let oldLocalStorage = JSON.parse(localStorage['accounts']);
// Do something to oldLocalStorage, which is the JS object
// Write back to local storage
localStorage.setItem(
"accounts",
JSON.stringify(oldLocalStorage)
);
Source:stackexchange.com