循环:For 循环、While 循环、ForOf 循环、ForIn 循环

2024-08-08 0 801

循环:For 循环、While 循环、ForOf 循环、ForIn 循环

循环的目的是重复一些功能。

一些类型的循环包括:

  • for 循环
  • while 循环
  • for…of 循环
  • for…循环

for循环

to 可以写一个简单的 for 循环如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

for (let i = 1; i

<p>要循环数组,我们可以执行以下操作:<br></p>

<pre class="brush:php;toolbar:false">const animals = ['lizard', 'fish', 'turtle'];

for (let i = 0; i

<p>我们还可以反向循环这个数组:<br></p>

<pre class="brush:php;toolbar:false">for (let i = animals.length - 1; i &gt;= 0; i--) {

  console.log(i, animals[i]);

}

// 2 'turtle'

// 1 'fish'

// 0 'lizard'

我们还可以循环内循环:

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

39

40

41

42

43

44

45

46

47

for (let i = 0; i

<p>如果我们想要迭代数组的数组,这很有用:<br></p>

<pre class="brush:php;toolbar:false">const seatingchart = [

  ['abigale', 'tim', 'cynthia'],

  ['bob', 'carter', 'zane', 'tanja'],

  ['quin', 'xavier'],

];

// to print each name individually from seatingchart:

for (let i = 0; i

<h2>

  

  

  while 循环

</h2>

<p>简单 while 循环的一个例子是:<br></p>

<pre class="brush:php;toolbar:false">let num = 0;

// to print out 0 through 4:

while (num

<h2>

  

  

  中断关键字

</h2>

<p>break关键字可用于退出while循环:<br></p>

<pre class="brush:php;toolbar:false">let input = prompt('say something:');

while (true) {

  input = prompt(input);

  if (input === 'stop copying me') {

    break; // finally stoPS prompting user

  }

}

它也可以用于退出 for 循环。假设我们有一行:

1

let line = ['abby', 'salvia', 'jamie', 'carter', 'john'];

我们想要输出 jamie 之前的每个人,但不输出 jamie:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

for (let i = 0; i

<h2>

  

  

  for...of 循环

</h2>

<p>如果我们想打印数组中的每个值,我们可以这样做:<br></p>

<pre class="brush:php;toolbar:false">let people = ['aGitha', 'bruce', 'charlie', 'dane', 'ernie'];

// to print each persons name:

for (let person of people) {

  console.log(person);

}

为了使之前的座位表示例更清晰,我们可以这样做:

1

2

3

4

5

6

7

8

9

10

11

12

const seatingchart = [

  ['abigale', 'tim', 'cynthia'],

  ['bob', 'carter', 'zane', 'tanja'],

  ['quin', 'xavier'],

];

// to print each name individually from seatingchart:

for (let row of seatingchart) {

  for (let person of row) {

    console.log(person);

  }

}

for…in 循环

如果我们想迭代对象中的每个键值对,我们可以这样做:

1

2

3

4

5

6

7

8

9

10

11

const testscores = {

  jim: 34,

  abby: 93,

  greg: 84,

  mark: 95,

  melvin: 73,

};

for (let person in testscores) {

  console.log(`${person} scored ${testscores[person]}`);

}

如果我们想使用 for…of 得到 testscores 的平均值,我们可以这样做:

1

2

3

4

5

6

7

let total = 0;

let scores = Object.values(testScores);

for (let score of scores) {

  total += score;

}

let avg = total / scores.length;

console.log(avg);

登录后复制

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 web前端 循环:For 循环、While 循环、ForOf 循环、ForIn 循环 https://www.xuezuoweb.com/9998.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用