1

I would like to remove an item from localstorage object:

addToFavs: [{"fav-name":"fav-787336"},{"fav-name":"fav-255890"}]

I stored the information, then I checked if the item found, I should remove it:

/*** getID is the items clicked ***/

var retrievedData = localStorage.getItem("addToFavs");
    
    var parseData = jQuery.parseJSON(retrievedData);

    $.each(parseData, function(x,y){
        if(y['fav-name'] == getID){
            console.log(getID+' found on '+x);
            console.log(parseData[x]);
            localStorage.removeItem(parseData[x]);
            console.log(x+' removed');
        } else {
            console.log(getID+' NOT NOT found on '+x);
        }

}); // end $.each

Thanks in advance

2
  • 2
    Use Array.prototype.filter() Commented Jun 20, 2020 at 21:02
  • How can I implement it in my code? I tried so many ways but failed Commented Jun 20, 2020 at 21:10

3 Answers 3

2

You cannot do this:

localStorage.removeItem(parseData[x]);

You need to either update the array you parsed, or create a new one, and then stringify it back to JSON, and setItem to update the value in localStorage. Because localStorage can only store Strings, not Arrays or Objects.

filter would be a nice way of doing it:

var jsonString = localStorage.getItem("addToFavs");
var arr = JSON.parse(jsonString);
// Filter to keep only those with a different ID than getID
arr = arr.filter(function(item) { return item['fav-name'] !== getID; });
// Store it back, stringified
localStorage.setItem("addToFavs", JSON.stringify(arr));
Sign up to request clarification or add additional context in comments.

Comments

1
  1. The value in the local storage is a string
  2. Read the local storage value by the key
  3. Decode the string value
  4. Apply the Array.prototype.filter function to filter the value out of the array
  5. Encode the array value
  6. Write it to local storage using the same key

Comments

0

Not to freak you out or something

addToFavs: [{"fav-name":"fav-787336"},{"fav-name":"fav-255890"}]. <---- in this line of code addToFavs is the item in local storage and not the object you stored.

and local storage only supports storage of type string:string as item:value, so

localStorage.removeItem(parseData[x]); <--- this line of code here is trying to look for an item, in your case

which is actually inside your stringified object that itself is value to addToFavs item,

hence you have to remove the item from the object and store the addToFavs again as illustrated below.

var ls = window.localStorage;
ls.setItem('addToFavs', '[{"fav-name":"fav-787336"},{"fav-name":"fav-255890"}]');

var getId = 'fav-787336';

var item = JSON.parse(ls.getItem('addToFavs'));

var updatedItems = item.filter(val => (val['fav-name'] === getId));

console.log(updatedItems);

//update loacl storage again

ls.setItem('addToFavs', JSON.stringify(updatedItems))

the code won't run as localStorage access is not granted dues to security concerns feel free to run code in browser console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.