当前位置:网站首页>What is the difference between for... In... And for... Of
What is the difference between for... In... And for... Of
2022-07-26 01:51:00 【Look at the universe】
for...in... and for...of...
1、 The difference in results
for…in… Traverse to get key( Get the attribute name )
for…of… Traverse to get value( Get the property value )
const arr = [" Ha ha ha ", 20,{
name:"www"}]
for(let val in arr){
console.log('for...in', val)
}
console.log('==== I'm the separator ====')
for(let val of arr){
console.log('for...of', val)
}
result :
2、 Continue to explore
for…in…( Traverse to prototype , Low efficiency )
Use for…in… Will traverse all enumerable properties of the array , Including prototypes , If you don't want to traverse prototype methods and properties , You can judge inside the loop , Use hasOwnProperty() Method can determine whether a property is an instance property of the object
var arr = [1,2,3]
Array.prototype.a = 123
Array.prototype.b = "for...in... Will traverse the prototype "
for(let index in arr){
console.log(arr[index],index)
}

for…of…( Non traversable objects )
for…of… Traversal is the value of the array element , and for of The traversal is only the elements in the array , Prototype properties and indexes are not included
var arr = [1,2,3]
arr.a = 123
Array.prototype.a = 123
for (let value of arr) {
console.log(value)
}
for of Applicable to passes / Array objects / character string /map/set/ Such as a collection of iterator objects , But you can't traverse objects , Because there is no iterator object .
A data structure only needs to be deployed Symbol.iterator attribute , Is considered to have iterator Interface , have access to for…of…
for of differ forEach,for of Yes. break,continue,return In combination with ,for of The loop can exit the loop at any time .
Traverse set
const set = new Set([10,20,30])
for(let n in set){
console.log("for in",n)
} //undefined
for(let n of set){
console.log("for of",n)
}

Traverse map、generator It's the same thing .for in Can't traverse , and for of Sure .
3、 Explore in depth
Which data structures are deployed Symbol.iterator attribute ?
The following data structures are iterator Interface :
- Array Array
- Map
- Set
- String
- arguments object
- Nodelist object , An array of class All deployed iterator Interface data structure can use array extension operator (…), And deconstruction assignment .
If I really want the object to use for of?
have access to Object.keys() Get the object's key Value set , In use for of.
var obj = {
name: 'www',age: 21, sex: 1};
console.log(Object.keys(obj)) // ['name', 'age, 'sex']
for(var key of Object.keys(obj)){
console.log(key, obj[key]);
// name www
// age 21
// sex 1
}
You can also deploy an object Symbol.iterator attribute .
边栏推荐
- Worthington核酸酶、微球菌相关研究及测定方案
- B - Krypton Factor (dfs+ backtracking method)
- “蔚来杯“2022牛客暑期多校训练营2 个人题解集合
- 大佬们, flinksql datahub源表,源表有字段 timestamp 16位, 写入Ora
- Dijkstra find the shortest path
- After reading this article, you should thoroughly understand how to do interface testing
- SVN版本控制分支、合并功能使用
- 旅行 (拆点分层)
- G2. passable paths (hard version) (tree diameter + LCA)
- Y77. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (VIII)
猜你喜欢

Speech comprehension - structural analysis exercise of fragment reading

Prime Ring Problem
![[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!](/img/52/8c1520db38ffa8927e975b6f244a65.png)
[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!

6 + 1 skills of Software Test Engineer

The best way to practice Animation: cover transition

SOC first project hello_ world

flutter 下 grpc list没有Setter 方法 ,如何使用相关属性

Practice sharing of monorepo based on yarn1.x

Recommend a super good UI automation tool: uiautomator2!

销量连连夺冠,五菱的成功秘诀只有低价吗?
随机推荐
NFT access tool premint was hacked and lost more than 370000 US dollars
登堂入室soc之arm汇编基础
Creation and management of MySQL database and table
The detailed knowledge summary of MySQL can be collected
推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!
pdf. JS introduction
Mark and lightbulbs (thinking)
Worthington产气荚膜梭菌神经氨酸酶的特征及测定
Understand Linglong platform unified access service from simple to deep Monet
Leetcode/ numbers that appear only once
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 d.[link with game glitch] two point answer +spfa ring
E. Split into two sets
In spark SQL, date is used to display the day of the week according to the year, month and day_ format(date,‘u‘)
6 + 1 skills of Software Test Engineer
Installing and using R in Anaconda
软件加群验证
Practice sharing of monorepo based on yarn1.x
How to use the pagoda panel to deploy the full stack project of node to the server
01. MySQL transaction isolation level and concurrent database access
网络之二三层转发