Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ตรวจสอบอีเมลและรหัสผ่าน - JavaScript

สมมติว่าเรามีข้อมูลผู้ใช้จำลอง:

const array = [{
  email: 'usman@gmail.com',
  password: '123'
}, {
  email: 'ali@gmail.com',
  password: '123'
}];

เราต้องเขียนฟังก์ชันรับ email และ password คืนค่า boolean ว่ามีผู้ใช้นี้ในระบบหรือไม่

ตัวอย่าง

โค้ดต่อไปนี้เป็นตัวอย่าง:

const array = [{
  email: 'usman@gmail.com',
  password: '123'
}, {
  email: 'ali@gmail.com',
  password: '123'
}];

const matchCredentials = (email, password) => {
  const match = array.find(el => {
    return el.email === email && el.password === password;
  });
  return !!match;
};

console.log(matchCredentials('usman@gmail.com', '123'));
console.log(matchCredentials('usman@gmail.com', '1423'));

ผลลัพธ์

จะแสดงผลในคอนโซลดังนี้:

true false