Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript Check Array with Object Keys using includes() but do not check the whole word

I have the following code. A body object and a sensitive word array.

If the body key contains any word from sensitive words, it should returns true.

const body = {
  name: "",
  email: "",
  otpp: ""
}
const sensitiveWords = ["password", "pin", "otp"]

const isSensitive = sensitiveWords.some(el => Object.keys(body).map(name => name.toLowerCase()).includes(el)) || false

console.log(isSensitive);
// returns: false
// expected result: true

But as you can see, the word otp is in the sensitive words, but its not matching otpp. I guess its because its looking for full string match.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need the above function to return true since the key contains otp in otpp.

Thank you.

>Solution :

You’re looking for

const containsSensitive = sensitiveWords.some(el =>
  Object.keys(body).some(name =>
//                  ^^^^
    name.toLowerCase().includes(el)
//                     ^^^^^^^^ string search
  )
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading