当前位置:网站首页>About for In and for Of understanding and use
About for In and for Of understanding and use
2022-07-22 09:47:00 【Yidamu salty alcohol】
Because I always forget how to use these two before , Write them down today , Leave a note for yourself , Also hope to help friends in need .
**
for…in
var arr1 = ["zou", "liu", "chen", "yang", "li", "wu"];
var arr2 = {
"name": " extra ",
"age": 24,
"sex": " male ",
"birth": "1996-03-06"
};
for (let i in arr2) {
console.log(i);
}
for (let j in arr1) {
console.log(j);
}
give the result as follows :
From this we can see that ,for…in Is to traverse the subscript value of an array or object , It's called key value ,
If you want to use for…in To traverse the values (value) If so :
var arr1 = ["zou", "liu", "chen", "yang", "li", "wu"];
var arr2 = {
"name": " extra ",
"age": 24,
"sex": " male ",
"birth": "1996-03-06"
};
for (let i in arr2) {
console.log(arr2[i]);
}
for (let j in arr1) {
console.log(arr1[j]);
}
Output :
What I want to say here is that I can use arr2.name This way to access value value , But in for…in In the loop, you cannot access and obtain the corresponding value value , Upper figure :
var arr2 = {
"name": " extra ",
"age": 24,
"sex": " male ",
"birth": "1996-03-06"
};
for (let i in arr2) {
console.log(arr2.i);
}
The result is four undefined, The specific reason is not very clear , Welcome friends who know to comment in the comment area below .
summary : If only to get key value , that for…in Still usable
for…of
var arr1 = ["zou", "liu", "chen", "yang", "li", "wu"];
var arr2 = {
"name": " extra ",
"age": 24,
"sex": " male ",
"birth": "1996-03-06"
};
for (let k of arr1) {
console.log(k);
}
for (let l in arr1) {
console.log(l);
}
result :
You can see for…of It can be used to traverse objects or arrays value It's worth it .
In addition, by the way forEach() This Method executes the provided function once for each element of the array , On business : Array .forEach(function( Array of value, Array of key, The array itself ){…})
forEach Use as needed , It's just a mention here
边栏推荐
猜你喜欢
When packaged in the form of build patches, the client cannot load metadata.
2022年 iuap 春季培训数据中台培训报道
[Datasheet ] 计量芯片CS5480数据手册解读
34. 两数相加
第二章 第二十六节:文件操作:文件修改
第二章 第十六节:字典的循环嵌套
Introduction to MVC framework
The error interface of EAS web page preview is blank
36.删除链表的倒数第 N 个结点
Main annotations used in the spingboot project
随机推荐
Wechat applet explains the usage of wx:if elif else (with view and block)
video实现控制视频进度和播放、暂停
EAS BOS custom export (including excel style setting, multi tab export, export file directory verification and backup)
24. 合并K个升序链表
Common functions of EAS extended report (self use)
关于最近面试总结
29.二叉树的右视图
Biochemistry review question I · protein
小白再国泰君安开户有没有什么安全隐患?
正则
setAttribute、getAttribute、removeAttribute
关于线程 thread (1)概念简介
《机器人SLAM导航核心技术与实战》第1季:第1章_ROS入门必备知识
生物化学复习题IV·脂类
Wechat applet text limit lines
产品切换国产芯片步骤,STM32F4切换GD32
36.删除链表的倒数第 N 个结点
第三章第四节:形参
Review of Biochemistry III. sugars
Servlet的生命周期