Loop through an array in JavaScript
答案1
Use a sequential for loop:
var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
alert(myStringArray[i]);
//Do something
}
@zipcodeman suggests the use of the for...in statement, but for iterating arrays for-in should be avoided, that statement is meant to enumerate object properties.
It shouldn't be used for array-like objects because:
- The order of iteration is not guaranteed, the array indexes may not be visited in numeric order.
- Inherited properties are also enumerated.
The second point is that it can give you a lot of problems, for example, if you extend the Array.prototype object to include a method there, that property will be also enumerated.
For example:
Array.prototype.foo = "foo!";
var array = ['a', 'b', 'c'];
for (var i in array) {
alert(array[i]);
}
The above code will alert, "a", "b", "c" and "foo!".
That be particularly a problem if you use some library that relies heavily on native prototypes augmention (such as MooTools for example).
The for-in statement as I said before is there to enumerate object properties, for example:
var obj = {
"a": 1,
"b": 2,
"c": 3
};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
// or if (Object.prototype.hasOwnProperty.call(obj,prop)) for safety...
alert("prop: " + prop + " value: " + obj[prop])
}
}
In the above example the hasOwnProperty method allows you to enumerate only own properties, that's it, only the properties that the object physically has, no inherited properties.
I would recommend you to read the following article:
答案2
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
arr = ["table", "chair"];
// solution
arr.map((e) => {
console.log(e);
return e;
});
Loop through an array in JavaScript的更多相关文章
- how to convert a number to a number array in javascript without convert number to a string
how to convert a number to a number array in javascript without convert number to a string 如何在不将数字转换 ...
- javascript: Jquery each loop with json array or object
http://www.codeproject.com/Articles/779303/JSON-and-Microsoft-technologies http://www.codeproject.co ...
- JavaScript json loop item in array
Iterating through/Parsing JSON Object via JavaScript 解答1 Your JSON object is incorrect because it ha ...
- [Algorithms] Sort an Array with a Nested for Loop using Insertion Sort in JavaScript
nsertion sort is another sorting algorithm that closely resembles how we might sort items in the phy ...
- [Javascript] Use a custom sort function on an Array in Javascript
Sorting in Javascript with sort uses lexical sorting by default, which means it will sort in alphabe ...
- How do you check if a variable is an array in JavaScript? [duplicate]
https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript ...
- How do I remove a particular element from an array in JavaScript?
9090down voteaccepted Find the index of the array element you want to remove, then remove that index ...
- [Javascript] The Array forEach method
Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...
- javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...
随机推荐
- Swift学习笔记(一):No such module 'Cocoa'
在xcode中创建一个Playground文件, 进行导包操作 ,import Cocoa 却提示No such module 'Cocoa' 原因是该Playground文件的platform 设置 ...
- Linux 的字符串截取很有用。有八种方法。
假设有变量 var=http://www.aaa.com/123.htm 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} 其中 var 是变量名,# 号是运算符,*// ...
- [Docker]学习笔记--搭建gitlab
Gitlab 是一个用于仓库管理系统的开源项目.使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 详细介绍可以参照官网,https://about.gitlab.com/ 今天主要是通过 ...
- 记录--java获取网络资源(图片、音频等)保存本地
注:本人开始运行下面报 java.io.FileNotFoundException ,纠结很久后清理tomcat后运行成功 //获取wav文件地址 String vRecordUrl=(request ...
- grafana-----Annotation
注释提供了一个方法,在丰富的活动中做个标志点.当你鼠标移到这个注释上,你可以得到title,tags,和text information的事件. Queries 注释事件获得是通过一个注释查询.打开d ...
- 烂代码 git blame
关于烂代码的那些事(上) - Axb的自我修养 http://blog.2baxb.me/archives/1343 关于烂代码的那些事(上) 六月 21, 2015 57 条评论 目录 [显示] 1 ...
- ssm框架整合-过程总结(第二次周总结)
距离上次写博客已经有4.5天的时间了. 这次写博客目的是总结一下项目开始到现在,过程中遇到的问题.和学到的知识.经验. 初略总结下自己从中学到的: Spring :在学习中被反复强调的Ioc(反转控制 ...
- 基于java mail实现简单的QQ邮箱发送邮件
刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 S ...
- python——列表&字符串互相转换方法小结
字符串(str)转列表(list) 转换方法:str.split() str = 'zhu gao chao' print(str.split(' ')) # 用split进行转换 str——> ...
- ABAP f4帮助输入多个值
*---------------------------------------------------------------------- * INITIALIZATION *---------- ...