javascript迭代_探索JavaScript迭代
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支持不同类型的循环。 本文将探讨for , for / in , while和do / 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);}//=> 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 twitter.
我希望这段简短的循环之旅可以帮助您更好地理解JavaScript中迭代的工作方式。 如果您对循环有任何疑问,或者只是想聊天,也可以通过twitter与我联系。
翻译自: https://www.freecodecamp.org/news/exploring-javascript-for-in-loops-bdfc226d8515/
javascript迭代
相关文章:

4 RACMulticastConnection 连接类
# RACMulticastConnection信号被多次订阅如果一个信号多次被订阅,那么代码块代码会多次被执行。objective-c// 创建信号RACSignal *sg1 [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {NSLog("网络请求…

ie6下常见的bug 调整页面兼容性
ie6下常见的bug 我们布局页面,首先符合标准,如何写一个页面的标准性? 但是ie6等浏览器本身就比较特殊,bug比较多,兵法云,知己知彼百战百胜。我们需要了解ie6的一些常见bug,这样,更好…

Cacti安装详细步骤
Cacti安装详细步骤 前提LNMP或LAMP架构已搭建完成 一、cacti概述 1. cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户。因此࿰…

为什么使用单页应用_为什么我讨厌您的单页应用
为什么使用单页应用by Stefan Tilkov斯蒂芬蒂尔科夫(Stefan Tilkov) 为什么我讨厌您的单页应用 (Why I hate your Single Page App) Okay, now that I have your attention, let me say that I don’t really hate your single page app. I just find it highly annoying, unl…

marquee实现文字移动效果;js+div实现文字无缝移动效果
1.marquee实现文字移动: <marquee width"220px;" scrollamount"5" onmouseover"this.stop()" onmouseout"this.start()" ><p style"letter-spacing:2px;width: 1px;">欢迎您登录拜博医疗口腔集团内部…

URAL 1203 Scientific Conference(贪心 || DP)
Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校,补多校的题目,刷一下一直薄弱的DP。多校如果有计算几何一定要干掉-。- 题意:给你N个报告会的开始时间跟结束时间&a…

5- RAC 集合 RACTuple RACSequence
RAC 集合 RACTuple RACSequence // 0 RACTuple 就是一个数组/*RACTuple 就是一个数组*/RACTuple *tp1 [RACTuple tupleWithObjects:"5",5,1, nil];RACTuple *tp2 [RACTuple tupleWithObjectsFromArray:["11","22","33"]];NSLog(&quo…

测试开发人员与开发人员_如何升级为开发人员
测试开发人员与开发人员by Will Hughes威尔休斯(Will Hughes) 如何升级为开发人员 (How to Level up as a Developer) Being a productive developer is something you can learn through experience, books, or trial and error. But, one of the best ways to become a prod…

ORA-00959: tablespace 'PSAPTEMP' does not exist
错误 : ORA-00959: tablespace PSAPTEMP does not exist 解决办法: CREATE TEMPORARY TABLESPACE PSAPTEMP TEMPFILE E:/Oracle/ORC/sapdata3/temp_1/temp.data1 SIZE 500M REUSE AUTOEXTEND ON NEXT 100M MAXSIZE unlimited EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;ALTER …

RAC rac_liftSelector
RAC rac_liftSelector 主要是用于线程的同步 - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.// rac_liftSelector// 类似于dispatch_group 中的组// 多线程中的组 等所有的请求都完毕之后 去更新UIRAC…

随笔记一些莆田话
莆田话是闽南话和福州话混合的产物,当然也是古汉语保留至今的珍宝。很多莆田话的词语是有源可溯的。这里记录一些平常想到的又可能不为人知的词语。 莆田话——普通话解释 物件——东西 万代——很多 先生——老师(先生白读时是老师的意思)&a…

JavaScript库和API
by Adam Recvlohe通过亚当雷夫洛厄(Adam Recvlohe) API就像一盒巧克力 (APIs are like a box of chocolates) If you have written JavaScript for the DOM before, then you probably know how unwieldy it can get. I mean getElementById is seven syllables and 14 charac…

Hadoop 全分布模式 平台搭建
现将博客搬家至CSDN,博主改去CSDN玩玩~ 传送门:http://blog.csdn.net/sinat_28177969/article/details/54138163 Ps:主要答疑区在本帖最下方,疑点会标注出来。个人在配置过程中遇到的困难都会此列举。 实验介绍: 本次实…

iOS 使用fastlane自动化打包步骤
加粗样式### iOS 使用fastlane 自动打包步骤 !参考 1 查看ruby版本信息 本机是否安装ruby ruby -v 2 安装xcode命令行工具 点击同意即可 xcode-select --install 3 安装fastlane 键入如下命令 sudo gem install fastlane -NV4 使用 1 打开终端 cd 进入到要打包的…

今天开始搞CentOS 7
今天开始搞CentOS 7,安装过程很顺利,界面相当友好。转载于:https://www.cnblogs.com/lixd/p/3868649.html

java ruby_Java,Ruby和Go,我的天哪!
java rubyFree Code Camp has focused 100% on full stack JavaScript since we started 17 months ago. We’ve taught JavaScript on the front end, JavaScript on the back end (thanks to the powerful Node.js framework) — and even JavaScript as a database querying…

http和https的区别 与 SSL/TLS协议运行机制的概述
http和https的区别 与 SSL/TLS协议运行机制的概述 参考1 1 http 是不使用的SSL/TSL的通信通道 窃听风险:第三方获取通信内容篡改风险:修改通信内容冒充风险:冒充他人身份参与通信 2 SSL/TSL 协议应运而生 客户端先向服务器端索要公钥&am…

Babel 相关资料
Babel online editorBabel Plugin Handbookbabeljs usage options转载于:https://www.cnblogs.com/skating/p/6125227.html

php中this,self,parent三个关键字
phpfunctionclass语言cthis,self,parent三个关键字从字面上比较好理解,分别是指这、自己、父亲。this是指向当前对象的指针(姑且用C里面的指针来看吧) self是指向当前类的指针 parent是指向父类的指针(我 们这里频繁使用指针来描述,是因为没有更好的语言来表达)根据…

大量数据转移_大量数据
大量数据转移by BerkeleyTrue由BerkeleyTrue 大量数据 (A Flood of Data) Free Code Camp’s data has been doubling each month, thanks to a flood of highly-active campers. This rising tide of data has exposed several weaknesses in our codebase.由于大量活跃的露营…

约瑟夫问题总结
题解在代码里~ #include <iostream> #include <iomanip> #include <list> using namespace std;int main() {int n, k, f[100];n 12; cin>>k;//链表做法,复杂度O(n*k)list <int> L;for(int i 1; i < n; i) f[i] i, L.push_back(i);list<…

iOS中的死循环
关于死循环 自己方法里面调用自己 在 vc 中的 viewDidLoad 方法中调用 [self viewDidLoad] 会导致程序崩溃。 原因是: 内存溢出。 函数调用栈, 函数调用的时候,sp 栈顶指针寄存器减对应的内存空间,栈内存开启对应的内存空间&…

机器学习算法基础知识
在我们了解了需要解决的机器学习问题的类型之后,我们可以开始考虑搜集来的数据的类型以及我们可以尝试的机器学习算法。在这个帖子里,我们会介绍一遍最流行的机器学习算法。通过浏览主要的算法来大致了解可以利用的方法是很有帮助的。 可利用的算法非常之…

javascript_JavaScript疲劳疲劳
javascript“The Universe is under no obligation to make sense to you.” — Neil deGrasse Tyson“宇宙没有义务对您有意义。” —尼尔德格拉斯泰森 Yes, JavaScript development is complicated.是的,JavaScript开发很复杂。 Yes, it will continue to get mo…

TCP/IP基础概念及通信过程举例
TCP/IP基础概念及通信过程举例 出现 上个世纪60年代,由于中央集中式网络的容灾性较弱,以美国国防部为中心的一家组织研究出分组交换网络。后来为了验证分组交换技术的实用性,ARPANET出现了,并且在3年内逐渐发展,由4个节…

iOS 利用dSYM定位crash
What is dSYM ? xCode 的每一次编译都会生成一个dsym文件,在其内部存储了16进制函数地址的映射。 在App实际执行的二进制文件中,是通过地址来调用方法,所以在App Crash 的时候,第三方工具会抓到函数崩溃调用栈。 通过…

OCP-052 053部分答案解析
OCP~052 9. GRANT ANY OBJECT PRIVILEGE(授予任何对象权限):允许被授权人将其本身不拥有的对象的对象权限授予他人,但不能授予自己。 10. ENABLE VALIDATE 无法输入违反约束的行,而且表中的所有行都符合约束 DISABLE NOVALIDATE 可以输入任何…

您的用户界面是您产品不会因心灵感应而谦卑的补偿
by Morten Just由Morten Just 您的用户界面是您产品不会因心灵感应而谦卑的补偿 (Your UI is your product’s humble compensation for not being telepathic) 拿一些产品,然后继续问“这是要补偿什么?” 最终您将得到相同的答案。 这个答案可能就是为…

9、 Struts2验证(声明式验证、自定义验证器)
1. 什么是Struts2 验证器 一个健壮的 web 应用程序必须确保用户输入是合法、有效的.Struts2 的输入验证 基于 XWork Validation Framework 的声明式验证:Struts2 提供了一些基于 XWork Validation Framework 的内建验证程序. 使用这些验证程序不需要编程, 只要在一个…

java中使用队列:java.util.Queue
在java5中新添加了java.util.Queue接口,用以支持队列的常见操作。该接口扩展了java.util.Collection接口。Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来添加元素,使用poll()来获取并移出元素。它们的优点是通过…