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

242
Visualizações
How do I fix the error '.has is not a function' when using JavaScript Map?

I have the following code that attempts to set a value in a Map:

class Trie {
  constructor () {
    this.trie = new Map()
  }

  insert(word) {
    let current = this.trie
    for (let alpha of word) {
      if (!current.has(alpha)) current.set(alpha, [])
      current = current.get(alpha)
    }
    current.word = word
  }
}

let trie = new Trie()
trie.insert('test')
console.log(trie.trie)

When I attempt to run it, I get the error .has is not a function. What am I missing here?

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

0

You're reassigning current to a non-Map value inside the loop, so on subsequent iterations, current.has will not work. Sounds like you need to change the [] to a new Map instead.

class Trie {
  constructor () {
    this.trie = new Map()
  }

  insert(word) {
    let current = this.trie
    for (let alpha of word) {
      if (!current.has(alpha)) current.set(alpha, new Map())
      current = current.get(alpha)
    }
    current.word = word
  }
}

let trie = new Trie()
trie.insert('test')
console.log([...trie.trie])

enter image description here

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