当前位置:网站首页>The difference and usage of JS for in loop and for of loop
The difference and usage of JS for in loop and for of loop
2022-06-29 08:36:00 【Imperial City Code】
1.for in
usage : Used to traverse objects , Array and string key
The code is :
<script>
var arr = [1,2,3,4,5,6];
for(let Aarry in arr){
console.log(Aarry); // Get index 0,1,2,3,4,5,6
console.log(arr[Aarry]); // Get index corresponding value 1,2,3,4,5,6
console.log(arr+arr); // Can't participate in computation Spliced 1,2,3,4,5,61,2,3,4,5,6
}
</script>- for in The obtained subscript is of string type , Cannot participate in set operation
- The traversal order may not be in the internal order of the actual array
- Will traverse all enumerable properties of the array Including the prototype approach
Array.prototype.method=function(){ console.log("hellow"); } Array.prototype.hes = " Hello " var arr = [1,2,3,4,5,6]; for(let Aarry in arr){ console.log(Aarry); // Get index and enumerable properties 0,1,2,3,4,5,6 ,methods,hes } - Usually requires cooperation hasOwnProperty() Method to determine whether a property is an instance property of the object , To get the prototype object out of the loop .
<script>
Array.prototype.method=function(){
console.log("hellow");
}
Array.prototype.hes = " Hello "
var arr = [1,2,3,4,5,6];
for(let Aarry in arr){
if(arr.hasOwnProperty(Aarry)){
console.log(Aarry); // Get index 0,1,2,3,4,5,6
}
}
</script>Therefore, it is generally used for in Traversing objects instead of arrays
2.for of
usage : Values used to iterate through arrays and strings Can't traverse object
The code is :
<script>
var obj = {
name:" Zhang San ",
age:"15",
sex:" male "
}
for(let self of obj){
console.log(self);// Report errors Because the object does not have a traversable interface
}
</script>- If you have to traverse ordinary attributes It can be used for in loop Or built-in Object.keys() Method
- Object.keys() Get an array of object instance properties , Does not include prototype methods and properties
var obj = {
name:" Zhang San ",
age:"15",
sex:" male "
}
for(let self of Object.keys(obj)){
console.log(self+":"+obj[self]); //
}
</script>however , I feel like I've done it many times
- Traversal array
<script>
var a = [1,2,3,4,5,6,7]
for(let arr of a){
console.log(arr); // Getting array value Not subscript
}
</script>foreach Function can also do But it cannot appear in the function return countine break sentence however for of Sure
for in Traversing objects
for of Traversal array
边栏推荐
猜你喜欢

机器人代码生成器之Robcogen使用教程

solidity部署和验证代理合约

Debugging nocturnal simulator with ADB command

A high-frequency problem, three kinds of model thinking to solve this risk control problem

实战回忆录:从Webshell开始突破边界

使用adb命令调试夜神模拟器

About the many to many cascading insertion of sqlsugar (the ID of the collection attribute cannot be obtained, so the intermediate table cannot be maintained)
![[hcie TAC] question 5-2](/img/a5/308aa2cced4cba59354c576a07e3c0.jpg)
[hcie TAC] question 5-2

Excel中VLOOKUP函数简易使用——精确匹配或近似匹配数据

1284_FreeRTOS任务优先级获取实现分析
随机推荐
Voice annotation automatic segment alignment tool sppas usage notes
802.11--802.11n协议 PHY
Feature selection: maximum information coefficient (MIC) [used to measure the degree of correlation between two variables X and y, linear or nonlinear strength, commonly used for feature selection of
Dialogue | prospects and challenges of privacy computing in the digital age
Introduction to taro
开户买基金,通过网上基金开户安全吗?-
语音信号处理-基础(一):声学基础知识
Notes mosaïque
图文详解JVM中的垃圾回收机制(GC)
NLP标注工具:Label Studio实现多用户协作打标
NP3 格式化输出(一)
Hook 简介
Want to open a stock account, is it safe to open a stock account online-
特征选择:最大信息系数(MIC;Maximal Information Coefficient)【用于衡量两个变量X和Y之间的关联程度,线性或非线性的强度,常用于机器学习的特征选择】
实战回忆录:从Webshell开始突破边界
Paddlenlp general information extraction model: UIE [information extraction {entity relationship extraction, Chinese word segmentation, accurate entity markers, emotion analysis, etc.}, text error cor
solidity部署和验证代理合约
智能硬件evt dvt pvt mp
ES6数据类型Map&Set
324. swing sort II / Sword finger offer II 102 Target value of addition and subtraction