javascriptのすべての循環は遍歴しています.

1986 ワード

forシリーズ
forサイクルはjs言語の最も原始的な循環遍歴方式であり、すべてのforサイクルは終了サイクルをサポートしています.JavaScriptは既知の四つの終了サイクル実行方法を提供しています.
  let a = 'hi'
  for(let i = 0 ;i < arr.length; i++){
      console.log(i) //    0,1
  }
1、for…in…for…of…
  • for...in...インデックスに戻り、for...of戻り値
  •   let a = 'start'
      for (let i in a){
          console.log(i)  //    0,1,2,3,4,5
      }
    
      let a = [1,2,3]
      for (let i in a){
          console.log(i)  //    0,1,2
      }
    
      let a = {one:1, two:2, three:3}
      for (let i in a){
          console.log(i)  //  key one,two,three
      }
    
      let a = 'start'
      for (let i of a){
          console.log(i)  //    s,t,a,r,t
      }
    
      let a = [1,2,3]
      for (let i of a){
          console.log(i)  //    1,2,3
      }
    
      let a = {one:1, two:2, three:3}
      for (let i of a){
          console.log(i)  // a is not iterable
      }
    
    // variable:               。
    // iterable:                  。
      for (variable of iterable) {
        statement
      }
    
  • for...in...エニュメレート・属性だけでなく、プロトタイプチェーンの継承されたエニュメレート・属性にも戻ります.for...ofはエニュメレート・属性に戻ります.Object.keysはエニュメレート・属性
  • にのみ戻ります.
    Array.prototype.newArr = () => { }
    let Arr = ['   ', 'white', '55 ']
    for(let i in Arr){
        console.log(i) //    、white、55 、newArr(     )
    }
    
    Object.keys(Arr)  //    、white、55 
    
    for(let i of Arr){
        console.log(i) //    、white、55 
    }
    
    ES 6のシリーズ
    1、forEach、some、everyは、配列方法です.
    foreachは、集合または配列を巡回するだけで、サブ要素の値を返します.
    continueをサポートしないで、return falseまたはreturn trueで代替します.
    breakをサポートしないで、try catch/every/someで代替します.
      let a = [1,2,3]
      a.forEach(e=>{
        console.log(e) // 1, 2, 3
      })
    
      let obj = {one:1,two:2,three:3}
      obj.forEach(e=>{
          console.log(e)
      })
      // obj.forEach is not a function
    
    2、map
    mapは新しい配列を返します.元の配列は変更されません.新しい配列の値は元の配列呼び出し関数の処理後の値です.
    let arr = [1,2,3,4,5,6]
    arr.map(v=>{
      return v > 1   //[false,true,true,true,true],      
    })