当前位置: 代码迷 >> 综合 >> for... of 遍历的基本操作
  详细解决方案

for... of 遍历的基本操作

热度:37   发布时间:2024-02-26 18:37:20.0
const arr=[100,200,300,400]for (const item of arr){console.log(item)}

for...of 拿到的是数组的元素不是下标

for...of可以取代foreach,foreach不可以终止循环,for..of可以用break终止循环

for (const item of arr){console.log(item)if(item>200){
?    break}
}

es2015中新增的set方法 for...of遍历

const s=new Set([1,2,4,5,8])
for(const i of s) {console.log(i);
}

map数据解构 for...of遍历

const m=new Map()
m.set('foo',123)
m.set('bar',456)for(const [key,value] of m){console.log(key,value);
}

 

  相关解决方案