let testArray = [20, "string", true, "hahha"];
- en bucle
El bucle for es en realidad una gramática estándar de estilo del lenguaje C.
for (var i = 0; i < testArray.length; i ++) {
console.log("数组的值:"+testArray[i]);
}
// 数组的值:20
// 数组的值:string
// 数组的值:true
// 数组的值:hahha
- para ... en bucle
for (let index in testArray) {
console.log("数组的下标:"+index);
}
/*
数组的下标:0
数组的下标:1
数组的下标:2
数组的下标:3
*/
- para ... de bucle
for (let value of testArray) {
console.log("数组的值:"+value);
}
/*
数组的值:20
数组的值:string
数组的值:true
数组的值:hahha
*/
- El bucle
forEach forEach es en realidad la sintaxis de bucle de JavaScript. TypeScript, como un superconjunto de sintaxis de JavaScript, por supuesto es compatible de forma predeterminada.
testArray.forEach((value, index, array)=>{
console.log("value:"+value+"--index:"+index);
console.log(array);
});
/*
打印日志:
value:20--index:0
(4) [20, "string", true, "hahha"]
value:string--index:1
(4) [20, "string", true, "hahha"]
value:true--index:2
(4) [20, "string", true, "hahha"]
value:hahha--index:3
(4) [20, "string", true, "hahha"]
*/
- cada ciclo
every
es elJavaScript
ciclo de la gramática,TypeScrip
t comoJavaScript
superconjunto de sintaxis, por supuesto, también se admite el predeterminado. Como no se puede devolverforEach
en iteration
, se puede utilizar en suevery
lugarforEach
.
every
Bucle, el cuerpo del bucle debe tenertrue
ofalse
devolver un valor.
testArray.every((value, index, array) => {
return true;//类似于continue,持续当前循环
});
testArray.every((value, index, array) => {
console.log("index:"+index+"--value:"+value);
return false; //类似于break,跳出当前循环
});
// 打印日志
// index:0--value:20
Iteradores y generadores de documentos oficiales de TypeScript