在Javascript数组中循环

时间:2015-03-16 16:11:31

标签: javascript arrays for-loop

我有一个简单的问题,我不确定为什么数组内容没有正确返回。我很确定这很简单,但不知怎的,我没有得到我想要的结果。场景是变量"比较"被设置为例如一个值"苹果"我正在循环到数组中,如果apple匹配到文本字段中的索引打印。它没有这样做,它总是说"不一样"值。对于有价值的狗它有效。它似乎到达最后一个数组,然后进行比较。请帮助。

以下代码

<!DOCTYPE html>
<html>
<body>

<script>
function myFunction() {
var text = "";
var i;
var arr = ["apple", "banana", "carrot", "dog"];
var compare = "apple";

for (i = 0; i < arr.length; i++) {

    if (arr[i] == compare) {text = "The value is " + arr[i] + "<br>";  }

    else if (compare == "" || compare == null) { text = "The value is blank"; }

    else if (arr[i] != compare) {text = "not the same"; }

else {text ="some error";}

    }

     document.getElementById("demo").innerHTML = text;
}
</script>
<p>Click the button to do a loop with a break.</p>

 <button onclick="myFunction()">Try it</button>


<p id="demo"></p>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

  

似乎它到达最后一个数组然后进行比较。请帮助。

实际上,是的,因为你永远不会停止循环。因此,您对document.getElementById("demo").innerHTML所做的所有分配都会被最后一项覆盖。

如果您想在找到匹配项时停止,请使用break突破循环。

如果您希望元素包含已发生的事件列表(我认为可能是您尝试做的事情,这很难说清楚),请构建列表在text中,然后在最后分配:

if (compare == "" || compare == null) {
    // Doesn't make sense to loop in this case, presumably
    text = "The value is blank";
} else {
    text = "";
    for (i = 0; i < arr.length; i++) {

        if (arr[i] == compare) {
            text += "The value matches " + arr[i] + "<br>";
            //   ^--- note the +=
        } else {
            text += "The value doesn't match " + arr[i] + "<br>";
            //   ^--- note the +=
        }
    }
}
document.getElementById("demo").innerHTML = text;

答案 1 :(得分:1)

&#13;
&#13;
function print(msg) {
  document.getElementById("demo").innerHTML += msg + '</br>';
}

function myFunction() {
  var text = "";
  var i;
  var arr = ["apple", "banana", "carrot", "dog"];
  var compare = document.getElementById('compare').value;
  if (!compare) {
    print('Compare is empty');
    return;
  } else {
    print('Comparing with ' + compare);
  }

  for (i = 0; i < arr.length; i++) {
    if (arr[i] == compare) {
      print("The value is at index " + i + " is " + arr[i]);
      return; //results found, break out of the for loop
    } else if (arr[i] != compare) {
      print("not the same");
    } else {
      print("some error");
    }
  }
  print("Could not find " + compare + " in array");
}
&#13;
<!DOCTYPE html>
<html>

<body>

  <script>
  </script>
  <p>Click the button to do a loop with a break.</p>

  <input type="text" id="compare" placeholder="Compare to" />
  <button onclick="myFunction()">Try it</button>


  <p id="demo"></p>

</body>

</html>
&#13;
&#13;
&#13;

出于性能原因,最好在循环开始之前验证compare的值。您可以使用breakcontinuereturn关键字突破循环。

答案 2 :(得分:0)

你永远不会打破for循环。在满足break;条件时,您必须使用if退出循环。

这是您的解决方案:http://jsfiddle.net/urahara/rvLyfsto/

和你的代码:

function myFunction() {
    var text = "";
    var i;
    var arr = ["apple", "banana", "carrot", "dog"];
    var compare = "apple";

    for (i = 0; i < arr.length; i++) {

        if (arr[i] == compare) {
            text = "The value is " + arr[i] + "<br>";
            break;
        } else if (compare == "" || compare == null) {
            text = "The value is blank";
            break;
        } else if (arr[i] != compare) {
            text = "not the same";
            break;
        } else {
            text = "some error";
            break;
        }

    }

    document.getElementById("demo").innerHTML = text;
}

干杯!