Skip to content
Advertisement

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

Advertisement

Answer

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;
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement