js 各種循環的區別與用法(for in,forEach,for of)


1,forEach循環   不能跳過或者終止循環

const a = ["a","ss","cc"]
    a.dd="11"
    a.forEach(index =>{
//        if (index ==='ss') {
//            break;
//        }   // 終止循環 如果終止循環會報錯
        console.log(index)
    }) // a ss cc

2,for in 循環   返回可枚舉的屬性

for(index in a){
   console.log(a[index])
} //a ss cc 11   // 返回可枚舉的屬性

3,for of 循環  es6用法 可終止循環

for(let index of a){
      if(index === 'ss'){
          continue  // break
      }
      console.log(index)
}//a ss cc

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM