1. js Array loop traversal.
Array loop variables, the first thing to think about is the for (Var i=0;i<count;i++) way. In addition, a simpler foreach method of 2 can be used. The ForEach function.
Both Firefox and Chrome's array types have a foreach function. Use the following:
<!--Add by oscar999--> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "Author" content= "oscar999" > </HEAD> <BODY> <script>varArryall = []; Arryall.push (1); Arryall.push (2); Arryall.push (3); Arryall.push (4); //Anonymous WayArryall.foreach (function(e) {alert (e); }) functionT1 (ARG) {alert (arg);}//non-anonymous modeArryall.foreach (t1,arryall);</script> </BODY> </HTML>
But the above, the code in IE does not work properly.
Because the array of IE does not have this method
The implementation of the above sentence is "undefined", that is, in IE, the Array does not have a foreach method.
3. Make IE compatible with the Foreach method
Since the IE array does not have a foreach method, we will add this prototype method to it manually.
//Array.foreach implementation for IE support: //Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEachif(!Array.prototype.forEach) {Array.prototype.forEach=function(callback, Thisarg) {varT, K; if( This==NULL) { Throw NewTypeError ("This is a null or not defined"); } varO = Object ( This); varLen = o.length >>> 0;//Hack to convert O.length to a UInt32 if({}.tostring.call (callback)! = "[Object Function]") { Throw NewTypeError (callback + "is not a function"); } if(thisarg) {T=Thisarg; } k= 0; while(K <Len) { varKvalue; if(kinchO) {Kvalue=O[k]; Callback.call (T, Kvalue, K, O); } k++; } }; }
Detailed introduction can be referred to:
Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach 4. How do I jump out of loops? Js This condition of the foreach can not use continue, break; You can use the following two ways: 1. The If statement controls 2. Return statement control (return TRUE or return false) actually return is similar to the role of continue
The following example is the number of multiples of 2 in the array and 3;
<!--Add by oscar999--> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "Author" content= "oscar999" > </HEAD> <BODY> <script>if(!Array.prototype.forEach) {Array.prototype.forEach=function(callback, Thisarg) {varT, K; if( This==NULL) { Throw NewTypeError ("This is a null or not defined"); } varO = Object ( This); varLen = o.length >>> 0;//Hack to convert O.length to a UInt32 if({}.tostring.call (callback)! = "[Object Function]") { Throw NewTypeError (callback + "is not a function"); } if(thisarg) {T=Thisarg; } k= 0; while(K <Len) { varKvalue; if(kinchO) {Kvalue=O[k]; Callback.call (T, Kvalue, K, O); } k++; } }; } varArryall = []; Arryall.push (1); Arryall.push (2); Arryall.push (3); Arryall.push (4); Arryall.push (5); Arryall.push (6); Arryall.push (7); varArryspecial = []; Arryall.foreach (function(e) {if(e%2==0) {Arryspecial.push (e); }Else if(e%3==0) {Arryspecial.push (e); } }) </script> </BODY> </HTML>
Use return to achieve the above effect
Arryall.foreach (function(e) { if(e%2==0) { www.2cto.com Arryspecial.push (e); return ; } if (e%3==0) { Arryspecial.push (e); return ; } })
As to how to write the effect of similar break, there is no better way to find it. Personal opinion: In both Java and C # syntax, foreach iterates through all the values and searches, some say return false can be reached, try, return false the same effect as return, and return ture is the same.
The following test code is my own Add.
var arryall = []; Arryall.push (1); Arryall.push (2); Arryall.push (3); Arryall.push (4); Arryall.push (5); Arryall.push (6); Arryall.push (7); Arryall.foreach ( function(e) {alert (e); if (e>3) return false ;});
Reference: http://www.2cto.com/kf/201303/195411.html
Http://www.cnblogs.com/mq0036/p/4015885.html
The Javascript array loops through the foreach