0

I have an array stored in localStorage. When a button is clicked, I would like the item in the array to be removed and again saved into localStorage. Here's what I mean.

var greetings = ["Hello", "Hi", "Sup", "Hey"]
localStorage.setItem("greetings", JSON.stringify(greetings))

function remove() {
  //remove "hi" How do I do this?
}
<button onclick="remove()">Remove</button>
  • You need to get the items from the array with localStorage.getItem('greetings') then you need to parse the string with JSON.parse(array). Then you need to filter out your preferred string: array.filter(item => item !== 'Hello') and then finally update the localstorage: localStorage.setItem("greetings", JSON.stringify(updatedArray)) – Shnigi May 04 '20 at 13:37
  • Does this answer your question? [Remove array item from localstorage](https://stackoverflow.com/questions/38748298/remove-array-item-from-localstorage) – Heretic Monkey May 04 '20 at 13:37
  • so the question is ... how do I remove an item from an array ... like `"Hi"` from `["Hello", "Hi", "Sup", "Hey"]`? ... btw ... should it be case(in)sensitive? – Peter Seliger May 04 '20 at 13:37
  • 1
    Removing an item from an array is covered in [How to remove item from array by value?](https://stackoverflow.com/q/3954438/215552) – Heretic Monkey May 04 '20 at 13:38

2 Answers2

1

You need to get the items from the array with localStorage.getItem('greetings') then you need to parse the string with JSON.parse(array) as localstorage items are always strings. Then you need to filter out your preferred string: array.filter(item => item !== 'Hello') and then finally update the localstorage: localStorage.setItem("greetings", JSON.stringify(updatedArray)

const greetings = ["Hello", "Hi", "Sup", "Hey"];
localStorage.setItem("greetings", JSON.stringify(greetings));

function remove() {
 const greetings = JSON.parse(localStorage.getItem("greetings"));
 const filtered = greetings.filter(item => item !== 'Hello');
 localStorage.setItem("greetings", JSON.stringify(filtered));
}
Shnigi
  • 511
  • 4
  • 15
0

I think You want this. Click here Hope it work or it would help little bit.enter code here

  • nope ... your example will fail for e.g. `var greetings = ["Hello", "Hi", "Hi", "Sup", "Hey"];` – Peter Seliger May 04 '20 at 14:06
  • ... the code also just works accidentally because for `greetings.splice(i,i);` and `var greetings = ["Hello", "Hi", "Hi", "Hi", "Sup", "Hey"];` ... `i` luckily happens to have the value `1` ... actually you wanted to write `greetings.splice(i,1);`? ... followed by `i--;`, with the latter fixing the issues. – Peter Seliger May 04 '20 at 14:18
  • Ok now i understood. look at this now https://codepen.io/Shbham07/pen/ZEbadQp – Shubham Rawat May 04 '20 at 14:39
  • yes same thing i did now in the code...... :) by just decrement the iteration count. – Shubham Rawat May 04 '20 at 14:41
  • The only think left in order to fulfill the OP's "definition of done" is to take out the item from the storage again, transform it into an array, process the array and store it's stringified version once again. – Peter Seliger May 04 '20 at 14:42