简体   繁体   English

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

[英]loop through all element in array before displaying alert

I want to check an array of usernames and passwords to authenticate user. 我想检查用户名和密码的数组以验证用户身份。 The code I have stops at every element of the array and displays alert. 我拥有的代码停在数组的每个元素上,并显示警报。 how can I have it loop through the entire array then show alert. 我如何让它遍历整个数组然后显示警报。

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")
  } 

I assume you would like to stop when you find the correct username password combination anyways. 无论如何,我认为您想停止时才找到正确的用户名密码组合。 So your ELSE is breaking the loop because it is executed on every run. 因此,您的ELSE会中断循环,因为它会在每次运行时执行。

Here is proposal: 这是建议:

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"); 
}

Now code will run and complete all elements in array, if user is found it will set success to true , and alter it otherwise it is false by default. 现在,代码将运行并完成数组中的所有元素,如果找到用户,则将success设置为true ,并对其进行更改,否则默认情况下为false。

Here's a more functional approach using find to search the user array for a match before continuing. 这是一种更具功能性的方法,可使用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");
}

Using Array some() Method: 使用Array some()方法:

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

Hope you are just testing your code including passwords in the client side . 希望您只是在客户端测试包含密码的代码。

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

Using a couple returns and moving down your alert outside of the if statement will help because return will kick out of the current iteration of the loop 使用几次返回并在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")
}

It is not clear if you want to show SUCCESS on all valid or on 1 valid user? 您不清楚要对所有有效用户还是对1个有效用户显示SUCCESS? And I pray that logic never ever reaches the world! 我祈祷逻辑永远不会传遍世界! :) :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM