繁体   English   中英

JavaScript-使用for循环和if-else语句遍历数组

[英]JavaScript - Using for loop and if-else statement to go through array

我正在尝试遍历数组,并检查用户的IP地址是否与我的clientip数组中的IP地址之一匹配。 在第一个循环(i = 0)之后,它立即跳转到else语句,并且不检查数组中的其他元素。 知道有什么问题吗? 我认为这是我的错。

<script>
        $(document).ready(function(){
            for(var i = 0; clientip.length; i++){
                if(clientip[i] === userip.toString() ){
                    console.log("Your IP is :", userip);
                    $("#showButtons").show();
                    break;
                }
                else{
                    console.log("Wrong ip address");
                    console.log(userip);
                    $("#showButtons").hide();
                    alert("You are not connected to the correct IP Address");
                    break;
                };
            };
        });
    </script>

谢谢

您最好尝试使用indexOf方法。

因此,如果将if语句更改为if(clientip.indexOf(userip.toString() ) != -1)if(clientip.indexOf(userip.toString() ) >=0)那么传送就可以了。 因此,您不需要break语句。

你有

打破;

在if和else中,它总是会在第一次尝试后中断循环,并且for中断了...

尝试:

<script>
    $(document).ready(function(){
        for(var i = 0; i < clientip.length; i++){
            if(clientip[i] === userip.toString() ){
                console.log("Your IP is :", userip);
                $("#showButtons").show();
                break;
            }
            else if (i === clientip.length -1) {
                console.log("Wrong ip address");
                console.log(userip);
                $("#showButtons").hide();
                alert("You are not connected to the correct IP Address");
                break;
            };
        };
    });
</script>

由于break处理,For循环在第一次迭代后停止

为什么在if和else语句中使用break? 尝试删除它们

同样,您可能想要设置一个标志,假设'found'变量将其初始值设置为false,并在if语句中将其设为true,以防万一在您的数组中找到了ip,然后,搜索,您可以使用if / else语句

if(found===true ){
   console.log("Your IP is :", userip);
   $("#showButtons").show();

 }
 else{
   console.log("Wrong ip address");
   console.log(userip);
   $("#showButtons").hide();
   alert("You are not connected to the correct IP Address");
};

否则,您的周期的每个迭代都会推送找到/未找到的消息

clientip.length将始终是真实的 (除非数组为空),因此您应该这样声明循环:

for(var i = 0; clientip.length < i; i++)

循环其余部分的逻辑有点古怪。 else子句处理以下情况:如果IP与数组中的任何内容都不匹配,但是直到循环完成,您才能知道,因此您不应将其放在其中:

        var found = false;

        for(var i = 0; clientip.length; i++){
                        if(clientip[i] === userip.toString() ){
                            console.log("Your IP is :", userip);
                            $("#showButtons").show();
                            // You found a match.  You're done.
                            found = true
                            break;
                        }

                    }
// No match was found.  
   if(!found) {
       console.log("Wrong ip address");
       console.log(userip);
       $("#showButtons").hide();
       alert("You are not connected to the correct IP Address");
       break;
   }

最后,不要在循环或if / else块之后放置分号。 充其量,它们什么也没做,最糟糕的是,它们会使您的代码变得时髦。

但是,有更简便的方法可以检查数组是否存在某个值:

if(clientip.indexOf(userip.toString()) != -1)
{
// userip was found
} else {
// userip was not found
}

暂无
暂无

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

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