为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第4天 for-in和for-of的区别?

第一模块:

课程名称:2周刷完100道前端优质面试真题
课程章节:第四章第十节 js中for-in和for-of有什么区别?
主讲老师:双越

第二模块:

课程内容概述

获取到key(0,1,2,3)
const arr = [10,20,30,40];
for(let key in arr){
	console.log(key);
}
获取到val(10,20,30,40)
const arr = [10,20,30,40];
for(let val of arr){
	console.log(val);
}

图片描述

适用于不同的数据类型

遍历对象: for-in可以
遍历Map Set、遍历generator:for-of可以

const set = new Set([10,20,30,40]);
for(let n of set){
	console.log(n);//10,20,30,40
}
const map = new Map([
	['x',100],
	['y',200],
	['z',300]
])
for(let n of map){
	console.log(n);//内容值
}

可枚举VS可迭代(原因对比)

for-in:用于可枚举数据,如对象、数字、字符串(enumerable: true),得到key
for-of:用于可迭代数据,如数组、字符串、Map、Set,得到value

补充:

for-await-of有什么作用?

const list = [p1,p2,p3];
// 方式一
Promise.all(list).then(res=>{
	console.log(res);
})
// 方式二
for await(let res of list){
	console.log(res);
}
// 场景:图片按需加载
// 方式三,执行第一个后再去执行第二个,顺序执行
for(let num of list){
	const res = await createPromise(num);
	console.log(re);
}

第三模块:

更加清晰地明白for-in和for-of 的区别
在工作中能够更加游刃有余

第四模块:

图片描述
图片描述
图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消