Iterating through the arrays in JavaScript

Source: Internet
Author: User

Here are the. Http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript

1. The best answer is the for-in is used to iterate over the properties of the object, which can be used to iterate over the array of problems, so the traditional for i=0; i<length;i++.
What I see in the code
var modules = [' AAA ', ' BBB '];for (var i in modules) {  if (Modules.hasownproperty (i)) {    //does something here  }}

If this goes through the array, then the value of I is the index of the array 0, 1, 2 ... However, if someone modifies the prototype of the array as follows, the for in will be hung out. :
Array.prototype.foo = "Foo!"; var array = [' A ', ' B ', ' C '];for (var i in array) {  alert (array[i]);}


2. Practices in ECMASCRIPT6 and 5:

To directly answer the question:usually. JavaScript is only have that capability if you ' re lucky enough to being in control of the JavaScript interpreter being used (USUA Lly not the case if it's browser-side code), and that implementation includes the For...of feature from the proposed sixth Version of the ECMAScript specification (code-named "Harmony"). If The stars has so aligned in your case, you can do this:

Only WORKS in ECMASCRIPT 6 "HARMONY" var s, myStringArray = ["Hello", "World"];for (s of myStringArray) {  //... do Something with S ...}

Or Better yet, since Harmony also provides block-scoped variables via let:

Only WORKS in ECMASCRIPT 6 "HARMONY" var mystringarray = [' Hello ', ' World '];for (let S of myStringArray) {  //... do Something with S ...} S is no longer defined

It seems that the subscript cannot be taken.

Most JavaScript programmers is working in a environment that's not there yet, however.

If you can assume the interpreter are compliant with version 5 of the specification (which means, for browser code , no versions of Internet Explorer before 9), then you can use the ForEach iterator method instead of a loop. In this case, you pass a function to is called for each item in the list:

var myStringArray = ["Hello", "World"];mystringarray.foreach (function (s,i) {      //... do something with s ...});

This is also good, as concise as underscore.


If you want something this works in All Versions of JavaScript, then you had to use an explicit counting loop. The safest version, which handles sparse arrays properly, is something like this:
var i, s, myStringArray = ["Hello", "World"], Len = mystringarray.length;for (i=0; i<len; ++i) {  if (i in Mystrin Garray) {    s = mystringarray[i];    ... do something with s ...  }

The traditional way

Assigning the length value to the local variable (as opposed to including the full mystringarray.length expression in the Loop condition) can make a significant difference in performance since it skips a property lookup each time through; Using Rhino on my machine, the speedup is 43%.

You'll often see the length caching do in the loop initialization clause, like this:

var i, len, myStringArray = ["Hello", "World"];for (len = mystringarray.length, i=0; i<len; ++i) {

The for...in syntax mentioned by others are for looping over a object ' s properties; since an Array in JavaScript are Ju St An object with numeric property names (and a magical "length" property), you can theoretically loop through an Array with It. But the problem's it doesn ' t restrict itself to the numeric property values (remember, even methods are actually Just properties whose value is a closure), nor does it iterate over those in numeric order. Therefore, the for...insyntax should not being used for looping through Arrays.

3. I want to try underscore JS
_ ([' AAA ', ' BBB ']). each (function (s,i) {    console.log (s,i);  })

or the library is handy to use.






Iterating through the arrays in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.