1.案例1
let arr = [1,2,3,4,5];
for(const value of arr){
console.log(value);
}
输出结果 1 2 3 4 5
2.案例2
data() {
return {
list: [
{
name: "帅帅1",
age: 11
},
{
name: "帅帅2",
age: 12,
some: 1
},
{
name: "帅帅3",
age: 13
},
{
name: "帅帅4",
age: 14
},
{
name: "帅帅5",
age: 15
},
{
name: "帅帅6",
age: 16
}
]
};
},
created() {
for (const s of this.list) { // 可以拿到每一项
console.log(s.name); // 帅帅1 帅帅2 帅帅3 帅帅4 帅帅5 帅帅6
}
for (const s in this.list) { // 只能拿到索引
console.log(s); // 0 1 2 3 4 5
}
for (const s in this.list) {
console.log(s.name); // undefined undefined undefined undefined undefined undefined
}
},
3.let 和 const 有什么区别
在于const 声明的变量不能重新赋值了,所以如果for-in 或for- of 中使用const 声明了变量( 如key), 循环体中,就不能给key 赋新值了,如果使用let ,那就无所谓了