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

95
Vistas
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 Respuestas
Responde la pregunta

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 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