简体   繁体   English

Javascript-如果条件循环指向数组,则在其中跳过循环代码

[英]Javascript - If loop skipping code inside, if the condition refers to an array

In example 1, running that results in line 4 being executed and then skipping right over line 5. In example 2, changing line 4 to refer to the actual value rather than an array allows line 5 to be executed. 在示例1中,运行该结果将导致执行第4行,然后直接跳过第5行。在示例2中,将第4行更改为引用实际值而不是引用数组将允许执行第5行。

What can I do to make example 1 work properly? 如何使示例1正常工作? And why does example 2 work and not example 1? 为什么示例2起作用而不是示例1?

I've used the same for loop in another instance, with a different array, that worked perfectly. 我在另一个实例中使用了相同的for循环,但使用了另一个数组,效果很好。 So perhaps it's the array itself that's the problem? 因此,问题可能出在数组本身上吗?

1. 1。

var temp2 = rightbarcoderead(i);

        for (i = 0; i < 10; i++) {
            if (temp2 === righteven[i]) {
                rightbarcode += i
            }

2. 2。

var temp2 = "100110";

        for (i = 0; i < 10; i++) {
            if (temp2 === righteven[i]) {
                rightbarcode += i
            }

From the information provided the only thing we know for sure is that in example 1 temp2 is not set with the expected value 100110 . 从提供的信息中,我们唯一可以确定的是,在示例1中, temp2没有设置为期望值100110

This may be due to i being undefined when you call 这可能是由于您致电时i未定义

var temp2 = rightbarcoderead(i);

or i set with the wrong value or, again, rightbarcoderead() function not returning the expected result with the given i parameter. i设置了错误的值,或者rightbarcoderead()函数未使用给定的i参数返回预期的结果。

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

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