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

95
Views
Javascript hash on a set of numbers

I need to get a frequency on a set of numbers and do some work on the "key" of the frequency, like check if the keys (as numbers) are consecutive. I am surprised that the key are of type "string". Is there a way to make the keys have type "number"?

of course, I can compare two keys like if (parseInt(k2) == (parseInt(k1) + 1)), but that's awkward.

Thanks

let freq = {} //frequency
let nums = [1,2,3]
nums.forEach(n => {
    if (freq[n] === undefined) {
        freq[n] = 1
    } else {
        freq[n] ++
    }
})
let uniq = Object.keys(freq)
console.log(typeof uniq[0])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a Map instead, which can have keys of any type - but it's slightly verbose too.

const freq = new Map();;
const nums = [1, 2, 3]
for (const num of nums) {
  freq.set(
    (freq.get(num) || 0) + 1
  );
}
const firstKey = [...freq.keys()][0];
console.log(typeof firstKey);

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!