Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
How to get the value from the localStorage object key? Angular

I need your help. In localStorage I have the object in the example. I am using Angular. I need to extract the value of the first linkedinMail key. I tried to do it in several ways, but as a result in the console, I get the first curly brace {

Object fron localStorage

{"linkedinMail": "mail.com", "password": "123"}

The first option:

public linkedinEmailBody: any

const emailLoginLinkedin = localStorage.getItem('credsForJetLeadBE')
let [key, value] = Object.entries(emailLoginLinkedin)[0];
this.linkedinEmailBody = value;
console.log(this.linkedinEmailBody) // result: `{`

The second option:

public linkedinEmailBody: any

const emailLoginLinkedin = localStorage.getItem('credsForJetLeadBE')
const valueEmailLinkedin = Object.values(emailLoginLinkedin)[0];
this.linkedinEmailBody = valueEmailLinkedin
console.log(this.linkedinEmailBody) // result: `{`

The third option

public linkedinEmailBody: any

const emailLoginLinkedin = localStorage.getItem('credsForJetLeadBE')
const valueEmailLinkedin = emailLoginLinkedin[Object.keys(emailLoginLinkedin)[0]];
this.linkedinEmailBody = valueEmailLinkedin
console.log(this.linkedinEmailBody) // result: `{`

When I use standard JS, the value of the linkedinMail field comes to me fine, instead of {. What am I doing wrong? Thank you very much

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

LocalStorage, sessionStorage stores strings. To change it to it original type, you have to parse it like this:

const emailLoginLinkedin = JSON.parse(localStorage.getItem('credsForJetLeadBE'));

After that you can use any way you want to retrieve the first key,value pair.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Local storage stores the value as a string, so you need to use

JSON.parse(localStorage.getItem('credsForJetLeadBE'))

to get the actual object.

This should work.

const emailLoginLinkedin = JSON.parse( // (1)
    localStorage.getItem("credsForJetLeadBE")
);

let [key, value] = Object.entries(emailLoginLinkedin)[0];
let linkedinEmailBody = value;
console.log(linkedinEmailBody);
  1. In local storage, you have a JSON string containing the object you're storing.

    In order for JavaScript to be able to work with it as an object, you need to call JSON.parse() on it.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'll provide you a whole example, because you not only could have a problem in the GET, but in the SET.

I'm not going to use the "easy way", in order you to understand better, and then (when it works fine), you can go wherever complicated you want (destructuring, ...).

// Initial Declaration
interface IEmailLoginLinkedin = {
   linkedinMail: string; 
   password: string;
}

emailLoginLinkedin: IEmailLoginLinkedin;

// Initialize 
 this.emailLoginLinkedin =  { linkedinMail: 'mail.com',password: '123' };


// Set value
this.setEmailLoginLinkedin(this.emailLoginLinkedin);

// Get values
const emailLoginLinkedinObject: IEmailLoginLinkedin|null  = this.getEmailLoginLinkedin();

let linkedinMail: string;
let password: string;

if (emailLoginLinkedinObject) {

   linkedinMail= emailLoginLinkedinObject.linkedinMail;
   password = emailLoginLinkedinObject.password;

   // Or the same but with 'destructuring':({linkedinMail, password}) = emailLoginLinkedinObject;

} else {
console.log('emailLoginLinkedin not set in the localStorage');
}

And the methods for the access to the localStorage:

 
// Serialize and STORE the emailLoginLinkedin in localstore:
setEmailLoginLinkedin(emailLoginLinkedin: IEmailLoginLinkedin){
   const emailLoginLinkedinString:string = JSON.stringify( emailLoginLinkedin );
   localStorage.setItem('emailLoginLinkedin', emailLoginLinkedinString);
}

----
 
// READ the emailLoginLinkedin from localstorage and Deserialize
getEmailLoginLinkedin(): IEmailLoginLinkedin|null {
 
   const emailLoginLinkedinString:string|null =  localStorage.getItem('emailLoginLinkedin');
 
   if( emailLoginLinkedinString ){
       return JSON.parse(emailLoginLinkedinString);
   } else {
       return null;
   }
 
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda