迭代输出一个数组的每个元素是 JavaScript 中的常见需求, for 循环可以做到这一点。

下面的代码将输出数组 arr 的每个元素到控制台:

var arr = [10,9,8,7,6];
for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}

记住数组的索引从零开始的,这意味着数组的最后一个元素的下标是:数组的长度 - 1。我们这个循环的 条件 是 i < arr.length,当 i 的值为 长度-1 的时候循环就停止了。

任务

声明并初始化一个变量 total0。使用 for 循环,使得 total 的值为 myArr 的数组中的每个元素的值的总和。

#221 Iterate Through an Array with a For Loop的更多相关文章

  1. [Javascript] Iterate Over Items with JavaScript's for-of Loop

    In this lesson we will understand the For Of loop in Javascript which was introduced in ES6. The for ...

  2. [Javascript] The Array filter method

    One very common operation in programming is to iterate through an Array's contents, apply a test fun ...

  3. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

  4. postgresql:array & foreach

    --数组: SELECT (ARRAY['{101, 111, 121}', '{201, 211, 221}'])[1]::text[]; SELECT (ARRAY['{101, 111, 121 ...

  5. Twitter OA prepare: Equilibrium index of an array

    Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...

  6. vector & array

    private static const NUM_LOOPS:int = 15; public function VectorTest():void { var vector:Vector.<i ...

  7. ros msg array

    #include <stdio.h> #include <stdlib.h> #include "ros/ros.h" #include "std ...

  8. [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  9. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

随机推荐

  1. 下拉js的实现

    这个JS是出自一个浴室柜网站 $(document).ready(function(){ $(".side_nav_3").hover(function() { $(this).f ...

  2. Ubuntu 通过apt安装VSCode

    1. sudo vim /etc/apt/sources.list.d/vscode.list 并向里面添加:deb [arch=amd64] http://packages.microsoft.co ...

  3. Android Toast 工具类

    android  中常用系统吐司工具类 package cn.yhq.utils; import android.content.Context; import android.widget.Toas ...

  4. python3 BeautifulSoup模块

    一.安装下载: 1.安装: pip install beautifulsoup4 2.可选择安装解析器:pip install lxmlpip install html5lib 3.解析器比较: 解析 ...

  5. git常用方法

    一.创建项目 git clone xx.git         克隆项目到本地 git init                     初始化本地项目,生成.git文件 二.创建分支,推送分支,合并 ...

  6. 定时执行自动化脚本-(一)导入保存jmeter参数至文件的jar包

    1.先完成jmeter脚本编写,具体怎么写此处不再详述,可以参考其他文档. 2.有时你会发现jmeter脚本执行后,有些响应信息你需要提取到文件中,以便后续使用: 怎么实现呢?个人思路就是,先写个ja ...

  7. Security.ssl-pinning

    SSL Pinning 1. What's SSL Pinning? "SSL Pinning is making sure the client checks the server’s c ...

  8. oracle异机恢复 open resetlogs 报:ORA-00392

    参考文档:ALTER DATABASE OPEN RESETLOGS fails with ORA-00392 (Doc ID 1352133.1) 打开一个克隆数据库报以下错误: SQL> a ...

  9. 3D Graph Neural Networks for RGBD Semantic Segmentation

    3D Graph Neural Networks for RGBD Semantic Segmentation 原文章:https://www.yuque.com/lart/papers/wmu47a ...

  10. bootstrap-table 踩坑手记

    官方文档: http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/ 渲染表格有两种方法: 1)data属性渲染表格 2)js代码渲染表 ...