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

336
Vistas
Why can't you assign a dictionary in a map operation in JS like this?

Can someone explain to me my error in logic for why this doesn't work to assign a key value pair in javascript and initialize their values to 0? The values return as undefined.

What is the best way to do this instead?

Example:

const letters = ['i','c','l','e'];
let dict = {};

dict = letters.map(value => dict[value] = 0);
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

map() returns an array of the results of the callback. So you're replacing the object with that array.

Since you're modifying the object in the loop, you don't need to assign back to the variable. And since you don't need an array of the results, you should use forEach() rather than map()

letters.forEach(letter => dict[letter] = 0);

BTW, in JavaScript we call these objects, not dictionaries.

about 4 years ago · Santiago Gelvez 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