js中关于const在for in或者for of中的使用

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 ,那就无所谓了

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值