首页 > web前端 > js教程 > 正文

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

王林
发布: 2024-08-07 22:00:50
转载
1001人浏览过

循环:for 循环、while 循环、forof 循环、forin 循环

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

一些类型的循环包括:

  • for 循环
  • while 循环
  • for...of 循环
  • for...循环

for循环

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

for (let i = 1; i <= 10; i++) {
  console.log(i); // prints numbers 1-10
}
登录后复制

要循环数组,我们可以执行以下操作:

const animals = ['lizard', 'fish', 'turtle'];

for (let i = 0; i < animals.length; i++) {
  console.log(i, animals[i]);
}
// 0 'lizard'
// 1 'fish'
// 2 'turtle'
登录后复制

我们还可以反向循环这个数组:

for (let i = animals.length - 1; i >= 0; i--) {
  console.log(i, animals[i]);
}

// 2 'turtle'
// 1 'fish'
// 0 'lizard'
登录后复制

我们还可以循环内循环:

for (let i = 0; i <= 2; i++) {
  for (let j = 0; j < 2; j++) {
    console.log(`i=${i}, j=${j}`);
  }
}

// i=0, j=0
// i=0, j=1
// i=1, j=0
// i=1, j=1
// i=2, j=0
// i=2, j=1
登录后复制

如果我们想要迭代数组的数组,这很有用:

const seatingchart = [
  ['abigale', 'tim', 'cynthia'],
  ['bob', 'carter', 'zane', 'tanja'],
  ['quin', 'xavier'],
];

// to print each name individually from seatingchart:
for (let i = 0; i < seatingchart.length; i++) {
  for (let j = 0; j < seatingchart[i].length; j++) {
    console.log(seatingchart[i][j]);
  }
}
登录后复制

while 循环

简单 while 循环的一个例子是:

let num = 0;

// to print out 0 through 4:
while (num < 5) {
  console.log(num);
  num++;
}
登录后复制

中断关键字

break关键字可用于退出while循环:

let input = prompt('say something:');
while (true) {
  input = prompt(input);
  if (input === 'stop copying me') {
    break; // finally stops prompting user
  }
}
登录后复制

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

let line = ['abby', 'salvia', 'jamie', 'carter', 'john'];
登录后复制

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

for (let i = 0; i < line.length; i++) {
  if (line[i] === 'jamie') break;
  console.log(line[i]);
}
登录后复制

for...of 循环

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

let people = ['agitha', 'bruce', 'charlie', 'dane', 'ernie'];
// to print each persons name:
for (let person of people) {
  console.log(person);
}
登录后复制

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

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 循环

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

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 的平均值,我们可以这样做:

let total = 0;
let scores = Object.values(testScores);
for (let score of scores) {
  total += score;
}
let avg = total / scores.length;
console.log(avg);
登录后复制

以上就是循环:For 循环、While 循环、ForOf 循环、ForIn 循环的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号