Skip to content Skip to sidebar Skip to footer

How To Check If Value Exist In Local Storage Data Set?

I have done some research about local storage. Seems like a pretty good option to store data. In my Single Page App I use JSON to store some of the data that is used more than once

Solution 1:

localStorage stores strings, which you know since you're already JSON.stringify()ing when you save. But you need to replace your check of

localStorage[recID].hasOwnProperty("L_NAME")

with

JSON.parse(localStorage[recID]).hasOwnProperty("L_NAME")

Edit: You have a whole object which you store as localStorage[recID], so you need to parse that, then access the resulting object, like:

const record = JSON.stringify({
  "F_NAME": {
    "value": "John"
  },
  "L_NAME": {
    "value": "Smith"
  }
});

console.log(JSON.parse(record)['F_NAME']['value'])

JSON.parse(record) becomes the object, then you access its descendant parameters ['F_NAME']['value']. The placement of the brackets is critical.

Post a Comment for "How To Check If Value Exist In Local Storage Data Set?"