都可以遍历数组,只有for…in 可以遍历对象
遍历数组
let arr = [12,33,55]
for(var i = 0;i<arr.length;i++){
console.log(i);
}
console.log('in---------------------arr');
for(var i in arr){
console.log(i);
}
console.log('of---------------------arr');
for(var i of arr){
console.log(i);
}
console.log('forEach---------------------arr');
arr.forEach(i=>{
console.log(i);
})
遍历对象
let data ={
nane:'张三',
sex:'男',
age:'22'
}
for(var i = 0;i<data.length;i++){
console.log(i);
}
console.log('in---------------------obj');
for(var i in data){
console.log(i);
}
console.log('of---------------------obj');
for(var i of data){
console.log(i);
}
console.log('forEach---------------------obj');
data.forEach(i=>{
console.log(i);
})
for…of遍历对象会报错
forEach遍历对象也会报错