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

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

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