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

227
Visualizações
create javascript map with two keys

I would like to create a map in JavaScript with two keys or need to know if there is a better solution. I need to know if key1 is part of the collection and if so get data1 and in another case I need to know if a value of type key2 is part of the collection and get its data. How do you do this in JavaScript?

Example:

const contacts = new Map();
contacts.set('key_person',  {phone: "617-555-4321", address: "321 S 2nd St"});

const contacts2 = new Map();
contacts.set('key_city',  {phone: "617-555-4321", address: "321 S 2nd St"});

At the moment I have maps with redundant information in order to get data by key_person and key_city. I want a joint map where I can filter for both keys at a time. Thanks you

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

0

I would create a simple class with such functionality.

class DoubleKeyedMap {
  constructor() {
    this.data1 = new Map();
    this.data2 = new Map();
  }
  set(key1, key2, value) {
    this.data1.set(key1, value);
    this.data2.set(key2, value);
  }
  get(key) {
    if (this.data1.get(key) !== undefined) {
      return this.data1.get(key);
    }
    return this.data2.get(key);
  }
  // TODO: clear, delete, has, keys, values, entries
}

const contacts = new DoubleKeyedMap();
contacts.set("key-person", "key-city", {phone: "617-555-4321", address: "321 S 2nd St"});
contacts.get("key-city");
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