当前位置:网站首页>Difference between for..in and for..of
Difference between for..in and for..of
2022-08-05 05:11:00 【Ability to learn the front-end】
Some time ago I encountered such a problem: using for..in and for..of casually brought me unpredictable consequences, which were a little different from what I thought.What exactly is the difference between for...in and for..of?When to use for..in?When to use for..of?Next, I will use code as an example to show the difference between the two.
First look at the difference between for...in and for...of when traversing an array:
As can be seen from the above, the console output results can be displayed normally. When traversing the array, for..in obtains the index value of each traversal item, while for...of obtains the index value of each traversal item.is the value of each traversal item.
Look at the difference between for..in and for..of when iterating over objects:
Through the console, we can see that when using for...in to traverse an object, each traversal will get the key value in the object, but when using for..of traversal, an error will be reported (obj is not iterable), translated as: the obj object is not iterable and cannot be used to traverse the object using for..of, it does not get the value of each traversal item.
Summary:
When traversing the array: for..in gets the index value of each item, and for..of gets the value of each item
When traversing objects: for..in gets the key value of each item (that is, the attribute name), while for..ofcannotFor traversing objects, an error will be reported
Extended content: Object.keys(obj) can get the keys value of obj and save it in the form of an array; Object.values(obj) can get the value of obj and save it in the form of an array
边栏推荐
猜你喜欢
University Physics---Particle Kinematics
Flutter learning 5-integration-packaging-publish
Basic properties of binary tree + oj problem analysis
Mini Program_Dynamic setting of tabBar theme skin
Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
虚证、实证如何鉴别?
Analyses the mainstream across technology solutions
flex布局青蛙游戏通关攻略
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
The log causes these pits in the thread block, you have to guard against
随机推荐
浅析主流跨端技术方案
span标签和p标签的区别
for..in和for..of的区别
UVA10827
u-boot调试定位手段
一篇博客通关Redis技术栈
判断语句_switch与case
Day14 jenkins部署
雷克萨斯lm的安全性到底体现在哪里?一起来看看吧
ESP32 485光照度
Day14 jenkins deployment
flex布局青蛙游戏通关攻略
The solution to the failure to read channel information when dedecms generates a message in the background
uva1325
Excel Paint
Understanding and use of C# on set() and get() methods
App快速开发建设心得:小程序+自定义插件的重要性
Is the NPDP certificate high in gold content?Compared to PMP?
how to measure distance from point to face in creo
dedecms织梦tag标签不支持大写字母修复