DataCent
DataCent

Reputation: 59

Check localStorage if item matches string

I want to proof if an item exists in localStorage, where the itemID matches a string, to remove it. The clear function works fine, but not without removing everything. Is there any way to do it?

    var myMatch = "test";
    localStorage.setItem("test_1", "value");

    -> localStorage.getItem(); // ?

Upvotes: 0

Views: 2264

Answers (1)

Vikasdeep Singh
Vikasdeep Singh

Reputation: 21756

localstorage has function getItem() instead of get().

So change:

localStorage.get(itemID);

To:

localStorage.getItem(itemID);

So your final code will be like below:

  var myMatch = "test";

  if (localStorage.getItem(itemID) === myMatch) {
    localStorage.removeItem(itemID)
  }

For more information in localStorage please check here.

Upvotes: 1

Related Questions