比较用户输入数组&对象

时间:2017-08-19 15:12:56

标签: javascript arrays

我需要将用户输入与设置的用户名和密码进行比较。现在这就是我提出的:

<system.webServer>
 <httpProtocol>  
    <customHeaders>  
     <add name="Access-Control-Allow-Origin" value="*" />  
     <add name="Access-Control-Allow-Headers" value="Content-Type" />  
     <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />  
    </customHeaders>  
  </httpProtocol>         
</system.webServer>

我需要以这种特定的方式来做,我无法弄清楚。

2 个答案:

答案 0 :(得分:0)

使用Array :: some

&#13;
&#13;
var sys = {
    users: [
        {
            user: 'user1',
            pass: 'qwerty'
        },
        {
            user: 'Ragnar',
            pass: 'lothbrok'
        },
        {
            user: 'guest',
            pass: 'guest'
        }
    ],
    valid: function () {
        let username = "Ragnar";
        let password = "lothbrok";
        return sys.users.some(u => u.user === username && u.pass === password);
    }
}

console.log(sys.valid())
&#13;
&#13;
&#13;

答案 1 :(得分:0)

为了清楚起见,请将您的用户名 - 密码组合作为单独的对象,并在不同的代码块中使用身份验证。

    //username-password objects
    var sys = {
    users: [
        {user: 'user1',  pass: 'qwerty'},
        {user: 'Ragnar',  pass: 'lothbrok'},
        {user: 'guest',  pass: 'guest'}
    ]}

    //take your inputs and store them in a variable
    var username = document.getElementById("userInp").value;
    var password= document.getElementById("passInp").value;
    var count = 0;
    for(i = 0; i< sys.users.length; i++)
    {
        if(sys.users[i].user == username && sys.users[i].pass == password)
            count++;
    }

    if(count>0)
        alert("correct credentials");
    else
        alert("incorrect credentials");

你的代码中已经有了很多,希望其余部分能够清除。