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

i have an array of objects i wanna check if an elements exist in this array javascript

i have this array of objects

data= [
     { name: "name1",
       email: "email1@gmail.com"},
     { name: "name2",
       email: "email2@gmail.com"},
     { name: "name3",
       email: "email3@gmail.com"},
]

i have the email on one of the users

"email2@gmail.com"

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 want to redirect the user in the users page if his email exist in the data array else redirect him to register page

>Solution :

const data= [
     { name: "name1",
       email: "email1@gmail.com"},
     { name: "name2",
       email: "email2@gmail.com"},
     { name: "name3",
       email: "email3@gmail.com"},
]

const email = "email2@gmail.com"

console.log(data.some(item => item.email === email))

returns true if found.
replace some with find to get the first element that matches

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