如何使用JavaScript循环遍历数组中的所有条目?
我以为它是这样的:
forEach(instance in theArray)
Run Code Online (Sandbox Code Playgroud)
theArray我的阵列在哪里,但这似乎是不正确的.
我有这个行代码:
_.each($scope.inspectionReviews, function (value, key) {
alert("status=" + value.IsNormal + " " + "name=" + value.InspectionItemName);
if (!value.IsNormal) {
$scope.status = false;
return;
}
$scope.status = true;
})
Run Code Online (Sandbox Code Playgroud)
在某些时候我想停止循环,但似乎返回不起作用.
我怎么能停止循环?
我有一个for循环,我想退出这样:
function MyFunction() {
for (var i = 0; i < SomeCondition; i++) {
if (i === SomeOtherCondition) {
// Do some work here.
return false;
}
}
// Execute the following code after breaking out of the for loop above.
SomeOtherFunction();
}
Run Code Online (Sandbox Code Playgroud)
问题是在// Do some work here.语句执行之后,我想退出for循环,但仍然希望执行整个for循环下面的代码(下面的所有内容// Execute the following code after breaking out of the for loop above.).
该return false语句确实退出for循环,但它也退出整个函数.我该如何解决?
我试图找到一些修复,但似乎没有找到我正在寻找的东西.也许是我的措辞.
所以我有这个表单,一旦它提交一个5位数的邮政编码,它检查一个数组的匹配值,一旦匹配我希望它显示成功消息,如果它不匹配然后返回不匹配,但目前它返回消息的时间与数组一样多.我仍然是相当新的JavaScript,所以任何帮助将不胜感激.提前致谢!
JavaScript的:
<script>
function getZip(e) {
e.preventDefault();
var zip = document.getElementById('zip').value; // Gets the value from the text field "zip" on the form.
var result = ["48650", "48611", "48746", "48614", "48706"];
for (var i = 0; i < result.length; i++) {
//console.log(result[i]);
//if (zip.length == 5 && zip == result[i]){
if (zip == result[i]) {
console.log(zip + " is = to " + result[i]);
alert('Match ' + zip);
} else if (result[i] != zip) {
alert("No Match1");
console.log(zip + …Run Code Online (Sandbox Code Playgroud)