skip to Main Content
 var testObject = { 'name': 1, 'email': 2, 'password': 3 };

 // Put the object into storage
 localStorage.setItem('testObject', JSON.stringify(testObject));

 // Retrieve the object from storage
 var retrievedObject = localStorage.getItem('testObject');

 console.log('retrievedObject: ', JSON.parse(retrievedObject));

I want to display only email or name how to do

2

Answers


  1. You can do it like this

    const testObject = { 'name': 1, 'email': 2, 'password': 3 };
    
    // Put the object into storage 
    localStorage.setItem('testObject', JSON.stringify(testObject));
    
    // Retrieve the object from storage 
    const retrievedObject = localStorage.getItem('testObject');
    
    //JSON.parse first
    const myObj = JSON.parse(retrievedObject);
    //Then access each key value separate
    console.log('name: ', myObj.name);//outputs: 1
    console.log('email: ', myObj.email);//outputs: 2
    
    Login or Signup to reply.
  2. const retrievedObject = JSON.parse(localStorage.getItem('testObject'));
    

    and then you access name and email like the following:

    const name = retrievedObject.name
    const email = retrievedObject.email
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search