javascript迭代

by Festus K. Yangani

由Festus K.Yangani

探索JavaScript迭代 (Exploring JavaScript Iteration)

Loops allow programs to perform repetitive tasks, such as iterating through an array, while adhering to the DRY principle (Don’t Repeat Yourself). They come in handy when you want to execute a function a number of times, using different sets of inputs each time.

循环允许程序执行重复性的任务,例如遍历数组,同时遵循DRY原理 (请勿重复自己) 他们 当您想多次执行一个函数时,派上用场,每次使用不同的输入集。

Just like other programming languages, JavaScript supports different kinds of loops. This article will explore for, for/in, while and do/while loops.

就像其他编程语言一样,JavaScript支持不同类型的循环。 本文将探讨forfor / inwhiledo / while循环。

For循环 (The For Loop)

The for loop is the most common style of JavaScript loop. Here’s its basic syntax:

for循环是JavaScript循环最常见的样式。 这是它的基本语法:

for (<initialization>; <condition>; <incremental expression>) {   code block // This is executed if condition evaluates to true}
  • initialization - used to declare new variables with the var keyword, typically used to initialize a counter variable (var i = 0).

    初始化 -用于使用var关键字声明新变量,通常用于初始化计数器变量(var i = 0)。

  • condition - An boolean expression to be evaluated before each loop iteration. If this expression evaluates to true, the inner commands will be executed.

    condition-在每次循环迭代之前要评估的布尔表达式。 如果此表达式的值为真,则将执行内部命令。

  • incremental expression - an expression evaluated at the end of each loop iteration. This is usually used to increment, decrement, or otherwise update the counter variable.

    增量表达 - 在每次循环迭代结束时计算的表达式。 通常用于递增,递减或以其他方式更新计数器变量。

Examples:

例子:

//Counting 1 to 5for (var i = 1; i <= 5; i++) {  console.log(i);}//=&gt; 1//=> 2//=&gt; 3//=> 4//=> 5
//Iterating through an arrayvar arr = [17, 22, 35, 54, 96];
for (var i = arr.length; i >=0; i--) {  console.log(arr[i]);}//=&gt; 96//=&gt; 54//=> 35//=> 22//=> 17

For / in循环 (The For/in Loop)

The for/in loop is used to iterate through properties of an object. A for/in statement looks as follows:

for / in循环用于迭代对象的属性。 for / in语句如下所示:

for (variable in object) {  statements}
  • variable - a different property name is assigned to this on each iteration.

    变量 - 每次迭代时,都会为此分配一个不同的属性名称。

  • object - the object whose enumerable properties are iterated through.

    对象 - 迭代其可枚举属性的对象。

Example:

例:

var myObj = {city: "Austin", state: "Texas", country: "USA"}
for (var key in myObj) {  console.log(myObj[key]);}//=&gt; Austin//=> Texas//=> USA

While循环 (The While Loop)

While loops are conditional loops where a condition is checked at the start of the iteration, and — if the condition is true — the statements are executed. Here’s the basic syntax of a while loop:

While循环是条件循环, 循环开始时检查条件,如果条件为true,则执行语句。 这是while循环的基本语法:

while (condition) {  statement //code block to be executed as long condition is true.}
  • condition - the expression evaluated before each iteration through the loop. If this condition evaluates to true, the inner commands are executed. If the condition evaluates to false, then the inner statement won’t execute and the program carries on.

    condition-在循环中每次迭代之前评估的表达式。 如果此条件的值为真,则执行内部命令。 如果条件的计算结果为false,则内部语句将不会执行,程序将继续执行。

  • statement - the code block to be executed as long as the condition evaluates to true.

    声明 - 的 只要条件为真,将执行的代码块。

Example:

例:

var i = 0;while (i < 3) {  console.log(i);  i++;}
//=>0//=>1//=>2

做/做 (The do/while)

The do/while loop is a variant of the while loop. Unlike in the while loop, do/while loop will execute the code block once, before it even checks to see whether the condition is true. Then it will repeat the loop as long as the condition is true.

do / while循环是while循环的变体。 与while循环不同, do / while循环将执行一次代码块,甚至在检查条件是否为真之前执行一次。 然后,只要条件为真,它将重复循环。

Syntax:

句法:

do {      statement //code block to be executed}while (condition);
  • statement - executed at least once, and is re-executed each time the condition evaluates to true.

    声明 - 至少执行一次,并在条件评估为true时重新执行。

  • condition - the expression evaluated after each iteration through the loop. If the condition evaluates to true, the statement is re-executed. If the condition evaluates to false, then execution of statement is stopped.

    条件 - 的 通过循环的每次迭代后评估表达式。 如果条件评估为true,则重新执行该语句。 如果条件评估为假,则语句的执行停止。

Example:

例:

var cars = ["Tesla", "Prius", "GMC", "Ford"];
var i = 0;do {      console.log(cars[i]);              i++;}while (i < cars.length)
//=&gt; Tesla//=> Prius//=> GMC//=> Ford

I hope this brief tour of loops has helped you better understand how iteration works in JavaScript. If you have any questions about loops, or just want to chat, you can also reach out to me on twitter.

我希望这段简短的循环之旅可以帮助您更好地理解JavaScript中迭代的工作方式。 如果您对循环有任何疑问,或者只是想聊天,也可以通过twitter与我联系

翻译自: https://www.freecodecamp.org/news/exploring-javascript-for-in-loops-bdfc226d8515/

javascript迭代

javascript迭代_探索JavaScript迭代相关推荐

  1. javascript 框架_克服JavaScript框架疲劳

    javascript 框架 by Tero Parviainen 通过Tero Parviainen 克服JavaScript框架疲劳 (Overcoming JavaScript Framework ...

  2. javascript教程_最好JavaScript教程

    javascript教程 JavaScript is the most widely used scripting language on Earth. And it has the largest ...

  3. javascript原型_使用JavaScript的示例报告卡Web应用程序原型

    javascript原型 Hi! At times, beginners always find it hard getting the application of the theory they ...

  4. javascript 模板_了解JavaScript中的模板文字

    javascript 模板 The author selected the COVID-19 Relief Fund to receive a donation as part of the Writ ...

  5. javascript 注入_注入JavaScript牟利:如何检测和阻止撇取者

    javascript 注入 In 2019 British Airways was fined a remarkable £183 million for a data breach of its s ...

  6. es6 依赖循环_探索 JavaScript 中的依赖管理及循环依赖

    我们通常会把项目中使用的第三方依赖写在 package.json 文件里,然后使用 npm .cnpm 或者 yarn 这些流行的依赖管理工具来帮我们管理这些依赖.但是它们是如何管理这些依赖的.它们之 ...

  7. javascript 符号_掌握javascript es6符号

    javascript 符号 JavaScript is one of the cores of web development. JavaScript, also known as ECMAScrip ...

  8. javascript 过滤_功能性JavaScript中的过滤和链接

    javascript 过滤 本文由Dan Prince , Vildan Softic和Joan Yinn进行了同行评审. 感谢所有SitePoint的同行评审员使SitePoint内容达到最佳状态! ...

  9. javascript解析_使用JavaScript解析数学表达式

    javascript解析 by Shalvah 由Shalvah 使用JavaScript解析数学表达式 (Parsing math expressions with JavaScript) A wh ...

最新文章

  1. 基础面试题 — 数据结构与算法
  2. python selenium 浏览器设置_python+selenium webdriver.firefox()方式配置浏览器设置
  3. hdu 5092 Seam Carving
  4. JDK8中好用的日期处理-LocalDate类-LocalTime-LocalDateTIme,mysql解决时区相差13小时的问题,日期格式器DateTimeFormatter
  5. 为什么Python是数据科学领域最受欢迎的语言
  6. 短线王的盯盘宝怎么样_2022考研英语韦林全程班怎么样?资源分享
  7. 手机firebug查看网页代码 How to View Website Source Codes on iPad / iPhone
  8. Python使用PyQT制作视频播放器
  9. 2018春节前新马泰游
  10. flex 错误信息类型及解决方法
  11. python opencv 界面按钮_如何使用Python构建简单的UI?
  12. eclipse不格式化注释
  13. 软件2班36人[扑林作]
  14. matlab选择语句函数,Matlab基本语句和基本函数
  15. 保障电邮EMAIL通讯的安全性
  16. VS2017 CUDA编程学习实例3:CUDA实现直方图统计
  17. 常用元器件使用方法5:RS-232电平转换芯片SP3232EEN的使用方法
  18. 北大计算机前辈徐,九年中获得国家最高科学技术奖的八位北大人
  19. vue elementui table表格序号一列 样式设计
  20. 服务器上的VGA切换原理,什么是VGA接口 原理及特点是什么

热门文章

  1. 如何用JavaScript操作form表单组件?
  2. switch的case使用数组C语言,使用常量数组的元素作为switch语句中的case
  3. python实现平衡二叉树_LeetCode 110. 平衡二叉树 | Python
  4. cmd查看所有数据库 db2_DB2数据库常用命令集
  5. python数字计算公式_Python中数字以及算数运算符的相关使用
  6. VUE还没生效,页面闪屏的问题解决办法 v-cloak
  7. React 父组件给子组件传值,子组件接收
  8. 10-18 JS基础复习笔记
  9. MFCard:易用的信用卡支付集成类库
  10. 美妆彩妆宣传PPT模板