当前位置:网站首页>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
边栏推荐
- 新版付费塔罗牌测算源码 (附带搭建教程)
- Seven common sorts
- NLP标注工具:Label Studio实现多用户协作打标
- Transformer details
- P6772 [NOI2020] 美食家(矩阵快速幂)
- 目标跟踪【单目标跟踪(VOT/SOT)、目标检测(detection)、行人重识别(Re-ID)】
- 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
- 各種級數(調和、幾何)總結
- Résumé des différentes séries (harmoniques, géométriques)
- 人民链鲍大伟:打破壁垒,建立全域数据治理共享及应用平台
猜你喜欢

Friends, static keywords, static methods, and relationships between objects

Matlab usage

Standard | China payment and clearing Association releases the first privacy computing financial specification

目标跟踪【单目标跟踪(VOT/SOT)、目标检测(detection)、行人重识别(Re-ID)】

The @dynamicmemberlookup and callasfunction features in swift implement the object transparent proxy function

Solidity deploy and validate agent contracts

Use GPU training in the cloud on the laboratory (take yolov5 as an example)

Explain the garbage collection mechanism (GC) in JVM

语音合成:概述【不等长序列关系建模的生成任务】

各種級數(調和、幾何)總結
随机推荐
Notes on key words in the original English work biography of jobs (VII) [chapter five]
Notice on organizing the second round of the Northwest Division (Shaanxi) of the 2021-2022 National Youth electronic information intelligent innovation competition
【无标题】
网上开股票账户真的安全吗?求答案
U盘内存卡数据丢失怎么恢复,这样操作也可以
mysql 主键约束删除问题
一个高频问题,三种模型思维来破解此风控难题
Chengtong network disk imitation blue playing network disk source code with video tutorial
NLP annotation tool: label studio realizes multi-user collaborative marking
Talking about Nacos configuration center from Nacos client
A method to quickly connect notebook computers to mobile phone hotspots
Np5 formatted output (III)
Introduction to taro
Transformer details
Some "non-technical" Thoughts on distributed digital identity
Matlab 用法
NP5 格式化输出(三)
The @dynamicmemberlookup and callasfunction features in swift implement the object transparent proxy function
标准|中国支付清算协会发布首个隐私计算金融规范
语音标注工具:Praat