当前位置:网站首页>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
边栏推荐
- [WeChat applet] WXML template syntax - conditional rendering
- Use IDEA to connect to TDengine server
- AUTOCAD - dimension association
- LAB Semaphore Implementation Details
- Requests the library deployment and common function
- 算法---一和零(Kotlin)
- Application status of digital twin technology in power system
- The underlying mechanism of the class
- mysql数据库表什么字段类型的存储长度最大?
- Homework 8.4 Interprocess Communication Pipes and Signals
猜你喜欢

upload upload pictures to Tencent cloud, how to upload pictures

RL reinforcement learning summary (1)
![[cesium] element highlighting](/img/99/504ca9802db83eb33bc6d91b34fa84.png)
[cesium] element highlighting

Application status of digital twin technology in power system

请写出SparkSQL语句

Dephi reverse tool Dede exports function name MAP and imports it into IDA

一篇博客通关Redis技术栈

Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource

App rapid development and construction experience: the importance of small programs + custom plug-ins

【cesium】加载并定位 3D Tileset
随机推荐
RL reinforcement learning summary (1)
number_gets the specified number of decimals
entry point injection
重新审视分布式系统:永远不会有完美的一致性方案……
UVA10827
数字孪生技术在电力系统中的应用现状
Flex layout frog game clearance strategy
jvm three heap and stack
Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource
说说数据治理中常见的20个问题
Excel Paint
The log causes these pits in the thread block, you have to guard against
Flutter 父子组件如何都能收到点击事件
[cesium] element highlighting
Basic properties of binary tree + oj problem analysis
In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit
Distributed systems revisited: there will never be a perfect consistency scheme...
1068 Find More Coins
Talk about 20 common problems in data governance
【学习笔记之菜Dog学C】动态内存管理之经典笔试题