在显示警报之前循环遍历数组中的所有元素

时间:2018-01-17 23:16:59

标签: javascript arrays

我想检查一组用户名和密码来验证用户身份。我的代码在数组的每个元素处停止并显示警报。我怎样才能让它遍历整个阵列然后显示警报。

for(i= 0 ; i< user.length; i+=1) {
  if (username === user[i].username && password === user[i].password) {

    alert("Log In Successful");

    document.getElementById("btn").style.display= "none"
    document.getElementById("navbar-user").innerHTML = username;
    modal.style.display= "none";
  } else {
      alert("Username or Password Incorrect")
  } 

6 个答案:

答案 0 :(得分:0)

我假设你想要在找到正确的用户名密码组合时停止。所以你的ELSE打破了循环,因为它在每次运行时执行。

以下是提案:

var success = false;

for(i= 0 ; i< user.length; i+=1) {
  if (username === user[i].username && password === user[i].password) {
     success = true; 
  } 
}

if(success){
    alert("Log In Successful");

    document.getElementById("btn").style.display= "none"
    document.getElementById("navbar-user").innerHTML = username;
    modal.style.display= "none";
}else{
    alert("Username or Password Incorrect"); 
}

现在代码将运行并完成数组中的所有元素,如果找到用户,它会将success设置为true,并更改它,否则默认为false。

答案 1 :(得分:0)

这是一种更实用的方法,使用find在继续之前搜索user数组中的匹配项。

let successfulLogin = user.find(u => {
    return u.username === username && u.password === password;
});

if(successfulLogin) {
    alert("Log In Successful");
    document.getElementById("btn").style.display = "none"
    document.getElementById("navbar-user").innerHTML = username;
    modal.style.display = "none";
} else {
    alert("Username or Password Incorrect");
}

答案 2 :(得分:0)

使用Array some()方法:

if (! user.some(u => username === u.username && password === u.password) )
    alert('Username or Password Incorrect');

答案 3 :(得分:0)

希望您只是在客户端测试代码,包括密码。

let alertSucc =true;
for(i= 0 ; i< user.length; i+=1) {
  if (username === user[i].username && password === user[i].password) {

      document.getElementById("btn").style.display= "none"
      document.getElementById("navbar-user").innerHTML = username;
      modal.style.display= "none";
  } else {
      alertSucc =false;
      
  } 
}
  alertSucc ? alert("Log In Successful"):alert("Username or Password Incorrect");

答案 4 :(得分:0)

使用一对返回并在if语句之外向下移动警报将有所帮助,因为return将退出当前循环的迭代

for(i= 0 ; i< user.length; i+=1) {
  if (username === user[i].username && password === user[i].password) {
    alert("Log In Successful");
    document.getElementById("btn").style.display= "none"
    document.getElementById("navbar-user").innerHTML = username;
    modal.style.display= "none";
    return;
  } else {
    console.log("We haven't proven whether true or false.
    return;
  } 
  alert("We did not find proper credentials that were true, and tested all inputs")
}

答案 5 :(得分:0)

目前尚不清楚是否要在所有有效或有效用户上显示SUCCESS? 我祈祷逻辑永远不会到达世界! :)

let userGOOD = [{username: "a", password: "b"}, {username: "a", password: "b"}];
let userBAD = [{username: "a", password: "b"}, {username: "a", password: "c"}];
let test = (username, password) => (user) => username === user.username && 
password === user.password;
let testAB = test("a", "b");
let GOOD = userGOOD.reduce((p, c) => p && testAB(c), true);
let BAD = userBAD.reduce((p, c) => p && testAB(c), true);