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

121
Visualizações
How to check if string matches any of the strings in the database

I'm trying to check if string matches any of the strings saved in database, but with the code I have right now it checks only the first one My code:

for (const key in keys) {
  if (keys[key].key !== hashedQueryKey) {
    return "Invalid Key provided.";
  } else return true;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You should not return if the key does not match as you want to continue comparing keys. Something like:

function queryMatches(keys, hashedQueryKey) {
 for (const key in keys) {
  if (keys[key].key === hashedQueryKey) {
    return true;
  }
 }
 return false;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You could use Object.keys and Array.contains() to check if your key is present

if (Object.keys(keys).contains(hashedQueryKey)) {
    return true;
} else {
    return "Invalid Key provided.";
}

although looking at your code, and being paranoid ... just in case your 'key' property differs from the objects' key, using Object.keys and anyMatch() is safer ...

if (Object.keys(keys).anyMatch(key => keys[key].key === hashedQueryKey)) {
    return true;
} else {
    return "Invalid Key provided.";
}

if you don't need the message - just true or false , then you can just return the predicate.

ie.

return Object.keys(keys).contains(hashedQueryKey);
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