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

131
Vistas
Is it possible in JS to assign in one line destructured object keys to another object?

My question is really simple but I have not been able to find an answer to this.

Currently I have this :

const {email, firstname, lastname, isAdmin} = decodeToken(token);
const user = {email, firstname, lastname, isAdmin};

I'm looking for a way to shorten this chunk of code into a single line. I tried different combinations but none worked.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think you can simply write:

const user = {email, firstname, lastname, isAdmin} = decodeToken(token);

So you can have the user object and the destructured fields accessible as variables in the scope.

UPDATE

This code as to be considered careful as @VLAZ pointed out in the comment, the destructured variables now are global. You can still avvoid this but you cannot have it in a single line as you requested:

let email, firstname, lastname, isAdmin;
const user = {email, firstname, lastname, isAdmin} = decodeToken(token);

If you don't need the destructured fields you can shorten even more:

const user = decodeToken(token);
about 4 years ago · Santiago Trujillo 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