Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda