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

243
Vistas
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 Respuestas
Responde la pregunta

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