1

I have stored an object with multiple properties in localStorage. It does not contain a single value like a string or a number. The name of the localStorage key is UserData and its value is the following object:

{
    key: "1287C31D714BE16FBD44D093E4173CFF"
    logTime: "20191013190439"
    operatorDni: "46653980"    
}

I need to retrieve the value of the object property operatorDni in order to perform some actions in my code. I have tried to retrieve it using this line of code:

operatorDni: string;

this.operatorDni= localStorage.getItem('UserData.operatorDni');

But I get null.

How can I get a property of an object in localStorage with the key? What am I doing wrong?

Thank you very much.

terahertz
  • 2,915
  • 1
  • 21
  • 33
Julio Rodríguez
  • 447
  • 1
  • 8
  • 23

1 Answers1

3

Use JSON.parse like

var userData= JSON.parse(localStorage.getItem('UserData'))
this.operatorDni=userData.operatorDni;

Also save it like

localStorage.setItem('UserData',JSON.stringify(yourObject))
jitender
  • 10,238
  • 1
  • 18
  • 44