Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda