Js 循环 forEach, map, for, for...in, for...of

for(let i=0;i<5;i++){

//循环内容

}

 

for...in遍历

const arr = [0,1,2,3,4];
for
(let index in arr) { console.log(index,arr[index]); };

 

for...of遍历


 const arr = [0,1,2,3,4];
for(let index of arr ) {  
   console.log(index);  
};  
// 遍历字符串
let str = "strstr"; 
for(let index of str) {  
   console.log(index); 
}

 

map遍历

const arr = [0,1,2,3,4];
let str = arr.map((i,index,arr) =>{
      console.log(this);
       return ++i;
    },this);
console.log(str);  //结果为 [1,2,3,4,5]

 

 forEach遍历

const arr = [0,1,2,3,4];
let str = arr.forEach((i,index,arr) => {
     sum += i;
     console.log("sum的值为:",sum); 
})   //执行5次,最终结果 10

 

Js 循环 forEach, map, for, for...in, for...of

上一篇:MySQL


下一篇:Innodb的b+树索引结构