hooyantsing's Blog

P32_for-in与for-of使用方法操作

字数统计: 292阅读时长: 1 min
2020/10/26

for-in与for-of使用方法操作

海豚日记 学习JS看这个就够了,全面打通 JAVASCRIPT 知识点(更新到17章)

for-in与for-of区别

for-in 遍历取列表数组的下标。

1
2
3
for (let index in list){
let value = list[index];
}

for-of 遍历取列表数组的值。

1
2
3
for (const obj of list){
let value = obj;
}

测试用例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<script>

/*
for-in 取键名(下标)
*/
let hd = [
{title:"第一章 走进JAVASCRIPT黑洞",lesson:3},
{title:"ubuntu19.10 配置好用的编程工作站",lesson:5},
{title:"媒体查询响应式布局",lesson:8},
];

document.write(`
<table border="1" width="100%">
<thead><tr><th>标题</th><th>课程数量</th></tr></thead>
`);
for(let i in hd){
document.write(`
<tr><td>${hd[i].title}</td><td>${hd[i].lesson}</td></tr>
`);
}
document.write(`
</table>
`);

/*
for-of 取值(对象)
*/
let houdunren = ["hdcms","houdunren.com"];

for (const iterator of houdunren){
console.log(iterator);
}

for (const iterable of "houdunren"){
console.log(iterable);
}

</script>

注意: ${} 是占位符,和EL表达式类似。

CATALOG
  1. 1. for-in与for-of使用方法操作
    1. 1.0.1. for-in与for-of区别