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

147
Visualizações
Can I use the same map key several times?

I'm storing and retrieving data like this:

data = new Map();
data.set("key", id);
data.set("key", id2);
data.get("key");

the second set is overwriting key element. I want a structure, that can hold multiple times the key element or one key element with multiple id data pairs. Does such a collection exist in Javascript or how to achieve this?

thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

JS Map and no other JS structure I'm aware of allows you to have more occurences of the same key. Keys must be unique.

I think that maybe unshifting new values into an array stored in a regular object might be a good way to achieve what you want. That way, your newest value will always be the first in the array, and thus accessible at index 0, like so:

const data = {key: []};

data.key.unshift(id);
data.key[0] // value of id

data.key.unshift(id2);
data.key[0] // value of id2
about 4 years ago · Juan Pablo Isaza 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