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

341
Visualizações
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 Respostas
Responde à pergunta

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