博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript迭代_探索JavaScript迭代
阅读量:2525 次
发布时间:2019-05-11

本文共 4541 字,大约阅读时间需要 15 分钟。

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 (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.

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

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 (
;
;
) { 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);}//=> 1//=> 2//=> 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]);}//=> 96//=> 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]);}//=> 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)
//=> 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 .

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

翻译自:

javascript迭代

转载地址:http://qbyzd.baihongyu.com/

你可能感兴趣的文章
Linux中对为本去重
查看>>
layui下拉框数据过万渲染渲染问题解决方案
查看>>
有序列表和无序列表
查看>>
Bootstrap文档
查看>>
【翻译】在Ext JS集成第三方库
查看>>
中华优秀传统文化教育的有效渗透
查看>>
计算机基础篇-01
查看>>
leetcode 58. Length of Last Word
查看>>
ios开发证书,描述文件,bundle ID的关系
查看>>
jsp之简单的用户登录系统(纯jsp)
查看>>
js计时事件
查看>>
EntityFramework 学习 一 Eager Loading
查看>>
dispatch_set_target_queue测试
查看>>
自己写的sql排序
查看>>
关于Mutex的笔记
查看>>
凸包1——卷包裹算法
查看>>
Centos 安装SVN并配置多个版本库
查看>>
Eos持久化实体
查看>>
Shell基础-通配符
查看>>
static类型的变量
查看>>