Best Way to Loop Through an Array in JavaScript
来源:互联网 发布:淘宝店的站内推广 编辑:程序博客网 时间:2024/05/16 07:54
http://www.sebarmeli.com/blog/2010/12/06/best-way-to-loop-through-an-array-in-javascript/
Last week, a colleague of mine asked me “Why are you using a ‘standard for loop’ rather than the faster ‘for-in loop’ to loop through an Array in JavaScript?” and for this reason I’m going to write a short explanation about that.
First of all, when I talk about the standard ”FOR loop”, I mean something like
instead the ”FOR-IN loop” is something like:
for-in loop is faster…just to type, for a lazy developer :) and it’s a common mistake especially for developers coming from Java programming (I’m a Java developer as well, that’s why I know that :P), trying to port Java into JavaScript.
The two main problems with for-in loop are : 1) It enumerates through all the properties even the ones associated to the Object (that can be augmented previously) 2) The order of the elements is not guaranteed.
You may not interested in the order of the elements(even if often you are), but you need to deal with the first issue and that’s what can happen:
The console will print not only the two elements from the array, but also the value of the new property in the Object. Remember that array are objects in JavaScript, at the root of the prototypal chain there is always Object and you never know if Object has augmented previously by a library or some script.
The correct way to execute that loop is using the for loop:
Actually, we can make a micro-optimization to this, “caching” the length of the array and avoiding every time to calculate the length of the array.
Notice that there is a comma between “var i=0” and “tot=myArray.length” as the “for loop” accepts three expressions.
If you find this last way a bit weird, you could do something like:
So it’s strongly recommended to use the FOR LOOP to loop through an Array and remember that Java and JavaScript are different.
In the next post, I may go through where the “for-in” loop is used.
- Best Way to Loop Through an Array in JavaScript
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- Is there a way to loop through a table variable in TSQL without using a cursor?
- Best way to sort an NSArray of NSDictionary's?
- CareerCup Sort an array in a special way
- A very efficient and economical way to shift an array
- Algorithm: efficient way to remove duplicate integers from an array
- Best way to detect integer overflow in C/C++
- How to define an array in c#.
- How to initialize an array in C
- An easy way to change colour in text control
- A simple way to read an XML file in Java
- An Efficient Way to Draw Approximate Circles in OpenGL
- What is the best way to create a good interface to an abstraction?
- Converting a array to an array of ranges in ruby
- Mac OS X: Best Way to Make an ISO from a CD or DVD
- Loop Through Object in Actionscript3.0
- How to format strings in an array in ruby?
- 无题
- 莱斯大学Python课程Mini-project#4 - "Pong"
- 北理计算机专硕 2014年 889数据结构
- AMD_Catalyst_14.4_泄漏版_byTimer0677
- ubuntu12.04安装深度音乐播放器和深度影音
- Best Way to Loop Through an Array in JavaScript
- 黑马程序员学习笔记——过河问题
- QT 学习之路--自定义信号槽
- CF--416B Art Union
- linux comm详解
- 陌生人的玫瑰花
- Qt 学习之路--Qt 模块简介
- Linux下批量修改名字方法
- 玩转2048,不如搞定2048