迭代输出一个数组的每个元素是 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. 2018-2019-20175302实验二《Java面向对象程序设计》实验报告

    2018-2019-2 学号实验二<Java面向对象程序设计>实验报告 一.实验步骤及内容 1. 面向对象程序设计 参考 http://www.cnblogs.com/rocedu/p/6 ...

  2. centos7下Etcd3集群搭建

    一.环境介绍 etcd主要功能是分布式的存储键值,优点不多说了,分布是集群,自动选举等等,自行百度,主要说下配置方法,折腾了几天,终于优点眉目了,记录下操作方法,本文参考了如下链接 https://w ...

  3. centos7上安装zabbix4.0

    zabbix4.0已经推出有一段时间了,针对之前版本做了很多优化配置,易用性得到提高,特别lts(long team support)长技术支持版本,官方说提供5年的稳定技术支持,在商业化运用上,是比 ...

  4. python的re模块详解

    一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...

  5. python的argpare和click模块详解

    一.argparse模块 1.模块说明 # argparse是python的标准库中用来解析命令行参数的模块,用来替代已经过时的optparse模块,argparse能够根据程序中的定义的sys.ar ...

  6. Codeforces Round #552 (Div. 3) F题

    题目网址:http://codeforces.com/contest/1154/problem/F 题目大意:给出n,m,k,n是物体的个数,m是优惠方式的种数,k是需要购买的物体个数, 然后给出n个 ...

  7. odoo 数据库到期提醒

  8. pycrypto 安装 Crypto 报错 error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools&quo ...

  9. Qt4.8.6开发WinCE 5.0环境搭建

    Qt-Wince5.0开发环境介绍 1.Windows7SP1 64 2.vs2008,以及sp1补丁 3.编译qt-everywhere-opensource-src-4.8.6.zip 4.qt- ...

  10. MySQL索引优化步骤总结

    在项目使用mysql过程中,随着系统的运行,发现一些慢查询,在这里总结一下mysql索引优化步骤 1.开发过程优化 开发过程中对业务表中查询sql分析sql执行计划(尤其是业务流水表),主要是查看sq ...