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

263
Vistas
Javascript: How to convert a Map to an Object that has an object as its keys?

let johnB = { name: "John Boy" },
    lilyA = { name: "Lily Allen" },
    peterD = { name: "Peter Drucker" };
    
const users = new Map([
  [johnB, 'boudler'], 
  [lilyA, 'rock'],
  [peterD, 'stone']
])

const obj = {};
users.forEach((value, key) => obj[key].name = value)

console.log(obj)

The above doesn't work but it shows the basic intent. I want to get name property from the map keys to be the key when the Map is "converted" to an object. Accessing just the key(without .name), javascript stringifies the object so you end up with [object, Object] as the key.

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

0

…an Object that has an object as its keys?

That does not exist. An object property cannot have an object as the key, it must be a string or a symbol. What are you attempting to achieve is simply not possible. Keep using the Map - that's what it is meant to be used for.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you just have your logic wrong in your foreach loop.. Is this what you were expecting?

{
  "John Boy": "boudler",
  "Lily Allen": "rock",
  "Peter Drucker": "stone"
}

let johnB = { name: "John Boy" },
    lilyA = { name: "Lily Allen" },
    peterD = { name: "Peter Drucker" };
    
const users = new Map([
  [johnB, 'boudler'], 
  [lilyA, 'rock'],
  [peterD, 'stone']
])

const obj = {};
users.forEach((value, key) => obj[key.name] = value)

console.log(obj)

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