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

97
Visualizações
Using Obejct as key inside an object

Is it possible to use object as a key inside an object?

I found the answer to be NO but with no clear explaination.
Example:

let user1 = { name: "John Doe" };
let numberOfAnswers = {}; // try to use an object

numberOfAnswers[user1] = 234; // try to use user1 object as the key
alert(numberOfAnswers) // [object][object]

But then what are we doing here? Why this seems to work? Are we not creating numberOfAnswers Object with user1 as key?

let numberOfAnswers = {
   user1: {
      visits: 123;
   }
} 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Is it possible to use object as a key inside an object?

No. Property keys are always strings or Symbols, never objects. If you use an object as a property key, it's implicitly converted to a string (usually) or Symbol (if the object overrides the "to primitive" operation).

You can use objects as keys in Map instances, though.

But then what are we doing here? Why this seems to work? Are we not creating numberOfAnswers Object with user1 as key?

let numberOfAnswers = {
   user1: {
      visits: 123;
   }
} 

No. You're creating an object with a property whose key is the string "user1" and whose value is an object.

about 4 years ago · Juan Pablo Isaza 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