Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Xác thực email và mật khẩu - JavaScript


Giả sử chúng ta có mảng giả chứa thông tin đăng nhập của hai người dùng:

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

Chúng ta cần viết hàm nhận vào email và mật khẩu. Trả về boolean dựa trên việc user có tồn tại trong cơ sở dữ liệu hay không.

Ví dụ

Dưới đây là đoạn code:

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'));

Kết quả console:

true false