var arr = [1,2,3,4,5];
for(let value of arr){
console.log(value);
}
//打印结果:依次输出:1 2 3 4 5
//可以用break来终止整个循环,或者continute来跳出当前循环,继续后面的循环
var arr = [1,2,3,4,5];
for(let value of arr){
if(value == 3){
//终止整个循环
break;
}
console.log(value);
}
//打印结果:1 2 ,用break实现了终止整个循环,不会继续后面的遍历
var arr = [1,2,3,4,5];
for(let value of arr){
if(value == 3){
//跳过当前循环,继续后面的循环
continue;
}
console.log(value);
}
//打印结果:1 2 4 5 , 用continue跳过当前循环,继续后面的循环
var arr = [1,2,3,4,5];
for(let index of arr.keys()){
console.log(index);
}
//打印结果:依次输出:0 1 2 3 4
let word = "你好世界";
for(let w of word){
console.log(w);
}
//打印结果:你 好 世 界
神奇的for..of 。
好不好用,试过才知道,别提多爽了。。。
(带来好运的微笑)
文章底部:你好,我的朋友!~,欢迎你O_o