回圈提供一个快速又简洁的方法来重复地做某件事,有了回圈,在取得资料时就方便许多。
当知道回圈要执行几次时使用
for 回圈可以用来执行一段程序码,当变数值与设定的条件符合时,就会持续执行到条件不符合才停止执行。
基本语法:
for ( 初始条件 ; 判断条件 ; 更新条件 ) {
要执行的程序码
}
例如:
for(let i = 0,i < 10,i++){
console.log(i);
}
/*0
* 1
* 2
* 3
* 4
* 5
* 6
* 7
* 8
* 9
* /
for(let i = 0,i <= 20,i+=5){
console.log(i);
}
/*0
* 5
* 10
* 15
* 20
*/
for(let i = 0,i < 5,i++){
if(i == 2){
continue;
}
console.log(i);
}
/*0
* 1
* 3
* 4
*/
for(let i = 0,i < 10,i++){
if(i == 5){
break;
}
console.log(i);
}
/*0
* 1
* 2
* 3
* 4
*/
当不知道回圈要执行几次时使用
务必要记得写上++,否则会造成无限回圈
let i = 0;
while(i < 10){
console.log(i);
i++;
}
/*0
* 1
* 2
* 3
* 4
* 5
* 6
* 7
* 8
* 9
* /
巢状回圈(回圈里包回圈)
let count = 0;
for(let i = 0,i < 5,i++){
for(let j = 0,j < 3,i++){
console.log(i,j);
count++;
}
}
console.log(count);//15
/*00
* 01
* 02
* 10
* 11
* 12
* 20
* 21
* 22
* 30
* 31
* 32
* 40
* 41
* 42
* /
i 从 0 到 4
j 从 0 到 2
i * j= 5*3 = 回圈运行 15 次
let friends=["John","Josh","Mike"];
for(let i = 0;i < friends.length;i++){
console.log(friends[i].toUpperCase())
}
//JOHN
//JOSH
//MIKE
用在 array 上
和 for loop 可以达到同样的效果
forEach 对阵列里的每个元素都执行 function
且在 forEach 里面每一次执行的 function 叫做 callback function
这个 callback function 里面可以放好几个参数 forEach(currentvalue,index,array)
在 forEach 的 function 不用取名字,这种没有名字的 function 又叫 Anonymous function
let luckyNumbers = [7,15,23,66,91,10,13];
luckyNumbers.forEach(function(n){
if(n > 20){
console.log(n);
}
});
//23 66 91
定义 : ISO 27001 ISMS 导入适用范围:机房管理、网路管理、BPM、ERP系统开发及维...
Hook的useRef函式用於function component有分两种方式做介绍, 请先在要使用...
mongoDB 除了可以让 line bot 使用之外,flask同样也可以去透过 pymongo ...
前言 今天我们要来处理 dashboard 的部分,但仅限於贴文的,留言的要留到明天。 /dashb...
大家好我是长风青云,今天不停被打扰QAQ。是有没有要让我录影片,不是打电话过来狂CALL,就是直接呼...