Skip to content
Advertisement

How to remove one value from a key in localstorage that has many values?

I’ve seen this question asked before but the solutions didn’t help me hence why i’ve asked it again.

Currently, I am storing values into an array and that array is getting stored into localstorage.

This is the object

JavaScript

I have mapped through this and used ‘name’ as the value. I am calling this value through a button using this function

JavaScript

and to remove the value from localstorage I am using this function.

JavaScript

Although the value is being removed from the array, it is not being removed from localstorage.

side note – I am calling this function with a separate button.

console log

JavaScript

But if I select another item to be added to localstorage, then the previous item gets removed.

JavaScript

I hope this makes sense, I tried to add detail to it as best as possible.

Advertisement

Answer

Try to use localStorage.removeItem method to remove item from storage:

JavaScript

UPDATE:

If an item is removed from array and we want to set this updated value to localstorage, then we can just update this value:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement