如何遍历对象并将结果与​​输入进行比较

时间:2017-07-15 21:48:35

标签: javascript arrays json loops object

我正在构建一个Web应用程序,需要构建一个登录站点。如何循环浏览此javascript对象并将登录表单上的用户输入(目前只需要电子邮件(“EML”))与数据进行比较以确保输入正确?我尝试制作一些东西,但只是想出了下面的for循环,这只是一团糟。 (该对象位于单独的js文件中) 谢谢!

function validate() {
    var un = document.login.username.value;
    var pw = document.login.password.value;
    var valid = false;

    for (var key in responseData) {
        if (responseData.hasOwnProperty(key)) {
            for (var i = 0; i < key.length; i++) {
                if (un == key[i]) {
                    valid = true;
                    break;
                }
            }
          }


    if (valid) {
        alert("Login was successful. Welcome, " + un + ".")
        window.location = "https://www.google.com";
        return false;
    }
}
var responseData = {
  authenticatUser: {  
   "ERR":0,
   "RSP":{  
      "AUTHC":"true",
      "USR":{  
         "ID":"2",
         "TJT":"FULL",
         "ACTV":"true",
         "BO":"1489760664786",
         "CONT":{  
            "FNM":"John",
            "LNM":"Doe",
            "PHN":"5556667777",
            "PHNTP":"NONE",
            "EML":"ex@mple.com",
            "EMLTP":"NONE"
         },
         "ADMIN":"false",
         "LLOGN":"1489760664786",
         "ACCT":{  
            "ID":"2",
            "TJT":"ID"
         }
      }
   }
},
    getUserAccountDetails: {  
   "ERR":0,
   "RSP":{  
      "ACCT":{  
         "ID":"2",
         "TJT":"FULL",
         "ACTV":"true",
         "BO":"1489760664786",
         "LU":"1489760664786",
         "NM":"Name",
         "DESC":"Description",
         "CONT":{  
            "FNM":"John",
            "LNM":"Doe",
            "PHN":"5556667777",
            "PHNTP":"NONE",
            "EML":"ex@mple.com",
            "EMLTP":"NONE"
         },
         "ADDRM":{  
            "STRT":"1 Miracle Way",
            "CITY":"San Antonio",
            "STATE":"Texas",
            "ZIP":"78245"
         },
         "ADDRB":{  
            "STRT":"1 Miracle Way",
            "CITY":"San Antonio",
            "STATE":"Texas",
            "ZIP":"78245"
         },
         "TZ":"US_CT",
         "LICS":"1",
         "REPOS":[  
            {  
               "ID":"2",
               "TJT":"ID"
            },
            {  
            }
         ],
         "USRS":[  
            {  
               "ID":"2",
               "TJT":"ID"
            },
            {  
            }
         ]
      }
   }
}

};

1 个答案:

答案 0 :(得分:0)

首先,您不应该在客户端验证登录详细信息或以纯文本格式保留客户详细信息,我假设您知道这一点,但您只是在学习。

for (var key in responseData) {
        // THIS IS REDUNDANT
        // YOU GOT THE KEY FROM THE OBJECT SO THIS ISN'T
        // NECESSARY 
        if (responseData.hasOwnProperty(key)) {
            // ITERATING OVER THE LENGTH OF THE KEY
            // DOESN'T MAKE A LOT OF SENSE
            // You're not in fact iterating over the object
            for (var i = 0; i < key.length; i++) {
                if (un == key[i]) {
                    valid = true;
                    break;
                }
            }
          }

您要做的是将json响应数据简化为

{
   "accounts" : [
      {
       "id" : "xxxxxxx",
       "username" : "xxxxxx",
       "password" : "xxxxxx"
      },
      {
       "id" : "xxxxxxx",
       "username" : "xxxxxx",
       "password" : "xxxxxx"
      },
      {
        "id" : "xxxxxxx",
       "username" : "xxxxxx",
       "password" : "xxxxxx"
      }
  ]
}

然后您将检查用户凭据,如下所示:

let data = JSON.parse(responseData);
found = false;
for(var account in data.accounts)
{
    if(account.username === un && account.password === pw)
    {
        found = true;
        break;
    }
}

if(found)
{
    //POST user id back to server to get details
}

请注意,我不建议以这种方式进行身份验证。如果你想要很好地模拟你应该运行某种类型的简单apache php或节点服务器,它将接受用户名和密码并验证服务器端并返回结果。