TypeScript_For bucle

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
    everyes el JavaScriptciclo de la gramática, TypeScript como JavaScriptsuperconjunto de sintaxis, por supuesto, también se admite el predeterminado. Como no se puede devolver forEachen i teration, se puede utilizar en su everylugar forEach.
    everyBucle, el cuerpo del bucle debe tener trueo falsedevolver 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

Supongo que te gusta

Origin blog.csdn.net/FlyingKuiKui/article/details/80547733
Recomendado
Clasificación