I have an Api which is fetching Email and password. After that I am seeing if that email and password exists by the following functions:
function EmailCheck(Email){
return arr.some(function(el) {return el.attributes.Email === Email;})}
And same for password just changing email to password but if email is of one object and password ob another objects it passing. Is there any way I can check if Password is in Object where Email is.
what do you mean by another object its passing? do your objects look like this
emailObject = {
'email': 'some@email.com'
}
passwordObject = {
'password': 'password'
}
or like this
authObject = {
'email':'some@email.com'
'password': 'password'
}
if it is like the latter you could just use one function
authCheck(email,password){
return arr.some((el)=>{
if(el.email != email){
return false
}
return el.password == password
})
}
however this may not be the best approach. it kind of depends on what your database looks like and how everything is strung together but what i have done in the past is something like
checkUser(email, password){
return arr.find((user)=>{
return user.email === email && user.password === password
})
}
that way it finds the first (should be only) object, and returns it to be used. this will also allow you to get the index in the array if needed or do ther checks on the same object once you have gotten it. (like removing the password check and doing that as a separate function)
checkUser(email, password){
let user = arr.find((user)=>{
return user.email === email
})
if(user){
//do password check and other operations here
}
return user // for anything that needs to be done with the user
}