简体   繁体   English

Javascript通过JSON循环对象s的数组

[英]Javascript Looping through JSON an array of object s

so i have a login form , and i want to check whether the entered name and password already exists in the JSON object , it will prompt a different message for both cases but thr problem is it's not doing it right , here's the code for more clarity : 所以我有一个登录表单,我想检查输入的名称和密码是否已经存在于JSON对象中,在两种情况下都会提示不同的消息,但是问题是它做得不正确,以下代码更加清晰:

     function registerInfo(){
     var name = document.forms[0].username.value;
     var pw = document.forms[0].pw.value

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var response = JSON.parse(xhttp.responseText);
        console.log(response);

    if (name === "" || pw === "") {
    alert ('please complete all the forms')
    } 

    for (var i = 0; i < response.loginfo.length; i++){
      if ( name === response.loginfo[i].username && pw === 
            response.loginfo[i].password) {
        alert('Welcome back ' + name);
        break;
        } // if statement curly braces
    } // loop curcly braces 

    for (var i = 0; i < response.loginfo.length; i++){
        if ( name != response.loginfo[i].username && pw != response.loginfo[i].pw){
        alert('Welcome here new ');
        break;
        } // if statement curcly braces 
        } // for 
      } // ready state if statement curly braces
    } // function curly braces
    xhttp.open("GET", "login.json", true);
    xhttp.send();

    return false;
   }

and here's the JSON Object for a quick test 这是用于快速测试的JSON对象

     {
      "loginfo" : [
      { 
          "username" : "moh",
          "password" : "lol"
      },
      {
          "username" : "zaki",
          "password" : "123"
      }         
      ]
      }

the problem is when the user enters the username and the password which exists in the JSON object id does alert "Welcome back!" 问题是当用户输入用户名,并且JSON对象ID中存在的密码确实警告“欢迎回来!” , but also the "welcome here new" , which i don't want to , since the latest alert is for new user which their credentials doesn't exist in the JSON object. ,还有我不希望的“欢迎来到这里”,因为最新的警报是针对新用户的,这些新用户的凭据在JSON对象中不存在。

Simplest fix would be not allows second for-loop to be executed, if a match is found, so 如果找到匹配项,最简单的解决方法就是不允许执行第二个for循环,因此

  • Either set a flag if match is found and don't execute second for-loop if that flag is set. 如果找到匹配项,则设置一个标志;如果设置了该标志,则不执行第二个for循环。

  • Return instead of break from first for-loop. Return而不是从第一个for循环中断。

But, you can make your code less verbose by using Array.prototype.some to check if any value matches 但是,通过使用Array.prototype.some来检查是否有任何值匹配,可以使代码的详细程度降低

var hasMatch = response.loginfo.some( s => s.username === name && s.password === pw );

hasMatch returns true if the match is found 如果找到匹配项,则hasMatch返回true

alert( "Welcome " + (hasMatch ? "back" + name : "here new" ) );

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

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