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

248
Views
JS: check existence of a full object using a key?

I am iterating through data and doing want to assign a random number to a fontObj only if it is unique

a fontObj looks like:

{
   postscript: "Calibri",
   style: "Bold",
   family: "Calibri"
}

I want to iterate through paragraphs in my code and use the fontObj as a key.

pseudocode:

if (fontMap[fontObj]) {
   console.log("Already found: " + fontObj + " and the random number is " + fontMap[fontObj])
} else {
   fontMap[fontObj] = Math.random()
}

what is the best way to structure that since I can't check the existence of an entire object using a key?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't use an object as a key, you have to use a string.

If the objects all have the same properties, simply make a string out of those values.

function fontObjToKey(fontObj) {
    return `${fontObj.postscript}/${fontObj.style}/${fontObj.family}`;
}

let fontKey = fontObjToKey(fontObj);
if (fontMap[fontKey]) {
    console.log("Already found: " + fontKey + " and the random number is " + fontMap[fontKey]);
} else {
    fontMap[fontKey] = Math.random();
}
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!