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

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

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 Denunciar

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