当前位置:网站首页>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
边栏推荐
- 虚证、实证如何鉴别?
- Flutter learning - the beginning
- human weakness
- UVA10827
- Flutter学习2-dart学习
- Redis - 13. Development Specifications
- The log causes these pits in the thread block, you have to guard against
- How to deal with DNS hijacking?
- dedecms error The each() function is deprecated
- Is the NPDP certificate high in gold content?Compared to PMP?
猜你喜欢
Excel Paint
Develop your own node package
Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
服务器磁盘阵列
大学物理---质点运动学
span标签和p标签的区别
upload upload pictures to Tencent cloud, how to upload pictures
About the installation of sklearn library
upload上传图片到腾讯云,如何上传图片
Basic properties of binary tree + oj problem analysis
随机推荐
Use IDEA to connect to TDengine server
【cesium】元素高亮显示
Day14 jenkins部署
Day14 jenkins deployment
虚证、实证如何鉴别?
MySQL中控制导出文件后变为了\N有什么解决方案吗?
服务器磁盘阵列
[WeChat applet] WXML template syntax - conditional rendering
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
算法---一和零(Kotlin)
MySQL基础(一)---基础认知及操作
The role of DataContext in WPF
Flutter真机运行及模拟器运行
Mini Program_Dynamic setting of tabBar theme skin
【无标题】
Cron(Crontab)--use/tutorial/example
Flutter学习2-dart学习
dedecms报错The each() function is deprecated
Reverse theory knowledge 4
8.04 Day35-----MVC three-tier architecture