JS中如何实现几种不同的循环结构与跳出机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计579个文字,预计阅读时间需要3分钟。
JavaScript中的循环是非常常用的一种控制结构,以下是几种常用的循环跳出方式总结:
1. for循环跳出javascriptvar arr=['q', 'w', 'e', 'r', 't'];for (var i=0, len=arr.length; i JS中的循环是大家很常用的,这里总结一下几种常用循环的跳出方式。 var arr = ['q','w','e','r','t'];
for(var i=0, len = arr.length ; i< len ; i++){
console.log(arr[i]);
}
// q , w , e , r , t
跳出本次循环continue: for(var i=0, len = arr.length ; i< len ; i++){
if(i == 2){
continue;
}
console.log(arr[i]);
}
// q , w , r , t
当i==2时,跳出本次循环,本次循环下面的代码不在执行。但是真个循环继续执行,直到循环条件为false。 跳出整个循环break:1.for循环
for(var i=0, len = arr.length ; i< len ; i++){ if(i == 2){ break; } console.log(arr[i]); } // q , w
当i == 2时,使用break跳出整个循环,后面的循环条件不在执行,直接退出整个循环。
2. for-in循环
退出方法同for循环。
3.jQuery的each循环
$.each(arr,function(index,oo){ console.log(oo); }) //q , w , e , r , t
退出当前循环 return true:
$.each(arr,function(index,oo){ if(index == 2){ return true; } console.log(oo); }) //q ,w ,r ,t
当index == 2 时,退出当前循环,整体循环继续执行。
退出整个循环 return false:
$.each(arr,function(index,oo){ if(index == 2){ return false; } console.log(oo); }); // q , w
当index == 2时,使用return false,可以退出整个循环,后面的条件不在执行。
4.forEach循环
arr.forEach(function(oo,index){ console.log(oo); }); // q, w, e, r, t
退出当前循环 return ;reutrn false ; return true ;
arr.forEach(function(oo,index){ if(index == 2){ return ; //return false; //效果同上 // return true; //效果同上 } console.log(oo); }); // q , w ,r ,t
在forEach循环中,return 返回任何值,都只能退出当前循环。
要想跳出整个forEach循环,可以使用抛异常的方式:
try{ arr.forEach(function(oo,index){ if(index == 2){ throw 'jumpout'; } console.log(oo); }); }catch(e){ } // q , w
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计579个文字,预计阅读时间需要3分钟。
JavaScript中的循环是非常常用的一种控制结构,以下是几种常用的循环跳出方式总结:
1. for循环跳出javascriptvar arr=['q', 'w', 'e', 'r', 't'];for (var i=0, len=arr.length; i JS中的循环是大家很常用的,这里总结一下几种常用循环的跳出方式。 var arr = ['q','w','e','r','t'];
for(var i=0, len = arr.length ; i< len ; i++){
console.log(arr[i]);
}
// q , w , e , r , t
跳出本次循环continue: for(var i=0, len = arr.length ; i< len ; i++){
if(i == 2){
continue;
}
console.log(arr[i]);
}
// q , w , r , t
当i==2时,跳出本次循环,本次循环下面的代码不在执行。但是真个循环继续执行,直到循环条件为false。 跳出整个循环break:1.for循环
for(var i=0, len = arr.length ; i< len ; i++){ if(i == 2){ break; } console.log(arr[i]); } // q , w
当i == 2时,使用break跳出整个循环,后面的循环条件不在执行,直接退出整个循环。
2. for-in循环
退出方法同for循环。
3.jQuery的each循环
$.each(arr,function(index,oo){ console.log(oo); }) //q , w , e , r , t
退出当前循环 return true:
$.each(arr,function(index,oo){ if(index == 2){ return true; } console.log(oo); }) //q ,w ,r ,t
当index == 2 时,退出当前循环,整体循环继续执行。
退出整个循环 return false:
$.each(arr,function(index,oo){ if(index == 2){ return false; } console.log(oo); }); // q , w
当index == 2时,使用return false,可以退出整个循环,后面的条件不在执行。
4.forEach循环
arr.forEach(function(oo,index){ console.log(oo); }); // q, w, e, r, t
退出当前循环 return ;reutrn false ; return true ;
arr.forEach(function(oo,index){ if(index == 2){ return ; //return false; //效果同上 // return true; //效果同上 } console.log(oo); }); // q , w ,r ,t
在forEach循环中,return 返回任何值,都只能退出当前循环。
要想跳出整个forEach循环,可以使用抛异常的方式:
try{ arr.forEach(function(oo,index){ if(index == 2){ throw 'jumpout'; } console.log(oo); }); }catch(e){ } // q , w
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

