hugh 的个人博客

Everyday is a new day

for...of和for...in的区别

看一个例子

var a = [1,2,3]
for(let i in a) {
  console.log(i)
}
//0
//1
//2

for(let i of a) {
console.log(i)
}
//1
//2
//3

注意点:

for...in 遍历的是可枚举属性, 应谨慎用于遍历数组等对象

for...in 遍历是无序的

var c={a:1,1:2,c:4,b:5}
for(let i in c) {
  console.log(i)
}
//1
//a
//c
//b

 

 


标题:for...of和for...in的区别
作者:hugh0524
地址:https://blog.uproject.cn/articles/2019/08/13/1565692129812.html