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

232
Vistas
How do I rename a key in a Map Object, while keeping the value and the other keys and values the same?
bookMap = new Map()
bookMap = Map(3) {
  'Horror': [
    { id: 9798721052927, title: 'Dracula', author: 'Brahm Stoker' },
    { id: 9798721052928, title: 'Dracula 2', author: 'Brahm Stoker' }
  ],
  'Romance': [
    { id: 9798721052933, title: 'Love Story', author: 'Brahm Stoker' }
  ],
  'Comedy': [ { id: 9797221052931, title: 'Ha Story', author: 'Brahm Stoker' } ]
}

const renameKey = (oldKey, newKey, { [oldKey]: old, ...others }) => ({
  [newKey]: old,
  ...others
})

nbm = renameKey('Horror', 'Cusisine', { ['Horror']: bookMap.get('Horror'), ...bookMap })


output

What do I put as ...others to get the ouput as bookMap with all 3 keys, not just the newly changed one?

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

0

Your function is designed for an object, not for a Map. A map destructured into an array of arrays. If you want to achieve the same functionality you can use a method like below :

const renameKey = (oldKey: string, newKey: string, map: Map<string, any>) => {
  const old = map.get(oldKey);
  const rest = [...map].filter(r => r[0] !== oldKey);
  return new Map(
    [[newKey, old],
    ...rest]);
}

Playground

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