https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript?page=1&tab=votes#tab-top

答案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://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript?page=2&tab=votes#tab-top

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的更多相关文章

  1. 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 如何在不将数字转换 ...

  2. javascript: Jquery each loop with json array or object

    http://www.codeproject.com/Articles/779303/JSON-and-Microsoft-technologies http://www.codeproject.co ...

  3. JavaScript json loop item in array

    Iterating through/Parsing JSON Object via JavaScript 解答1 Your JSON object is incorrect because it ha ...

  4. [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 ...

  5. [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 ...

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

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

  8. [Javascript] The Array forEach method

    Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...

  9. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

随机推荐

  1. Linux之(tomcat)服务之服务调优

    Tomcat调优原则: ● 增加连接数 ● 调整工作模式 ● 启用gzip压缩 ● 调整JVM内存大小 ● 作为web服务器时,与Apache或者Nginx整合 ● 合理选择垃圾回收算法 ● 尽量使用 ...

  2. python MD5操作

    def my_md5(str): import hashlib new_str = str.encode() #把字符串转成bytes类型 # new_str = b'%s'%str #把字符串转成b ...

  3. C语言练习题

    C语言练习题 1. 以下选项中,合法的一组C语言数值常量是(     ) A)028  .5e-3  .0xf B)12.  OXa23   4.5e0 C).177   4e1.5  Oabc D) ...

  4. echarts+thinkphp 学习写的第一个程序

    一.前台 建个名为map.html,代码如下. <!doctype html><html lang="en"><head> <meta c ...

  5. 二维码及二维码接合短URL的应用

    二维码 1.什么是二维码? 二维条形码,最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0 ...

  6. UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按

    #import "RootViewController.h" @interface RootViewController (){    UIImageView *imageView ...

  7. iOS 面试题总结

    最近项目做完了 比较空闲 在网上看了一份面试题 想自己整理一下 一.为什么说Objective-C是一门动态的语言?NSUInteger和NSInteger 的区别? 静态 动态是相对的,这里的动态语 ...

  8. 原生JavaScript写AJAX

    前端JavaScript: function ajaxGet(url, obj) { var request; if(window.XMLHttpRequest) { request = new XM ...

  9. 修改本机域名localhost为任意你想要的名称

    web项目研发中,测试的时候项目路径与发布以后的路径不一致,项目组之间的路径不一致,这样会加大工作量,这个时候我们可以统一一下开发的路径,这样可以省很多事,话不多说,看下面教程: 在系统盘中的如下路径 ...

  10. 关于python代码是编译执行还是解释执行

    Python 是编译型语言还是解释型语言?回答这个问题前,应该先弄清楚什么是编译型语言,什么是解释型语言. 所谓编译执行就是源代码经过编译器编译处理,生成目标机器码,就是机器能直接运行的二进制代码,下 ...