J
J
jedifa2021-06-11 19:25:11
JavaScript
jedifa, 2021-06-11 19:25:11

How to delete a specific value in the localstorage array?

I have values ​​in localstorage by key cartItems in the form of an array ["1", "2"] how can I specifically delete, for example, the second value, that is, "2"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-06-11
@jedifa

Only strings are stored in storage. Text.

  1. you need to pull the string from storage, this is JSON,
  2. parse string JSON.parse(str)and get array
  3. find the index of the element to be removedindexOf(valueToDelete)
  4. remove splice()this element
  5. convert array to stringJSON.stringify(arr)
  6. save to storage setItem(key, value)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question