Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!