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 can be used
2. The ForEach function.
Both Firefox and Chrome's array types have a foreach function. Use the following:
[HTML]View Plaincopy
- <!--ADD by oscar999-->
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en">
- <HTML>
- <HEAD>
- <title> New Document </title>
- <META name= "Author" content="oscar999">
- </HEAD>
- <BODY>
- <script>
- var arryall = [];
- Arryall.push (1);
- Arryall.push (2);
- Arryall.push (3);
- Arryall.push (4);
- Arryall.foreach (function (e) {
- Alert (e);
- })
- </Script>
- </BODY>
- </HTML>
But the above, the code in IE does not work properly.
Because the array of IE does not have this method
[JavaScript]View Plaincopy
- alert (Array.prototype.forEach);
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 array of IE does not have a foreach method, we will add this prototype method to it manually.
[JavaScript]View Plaincopy
- Array.foreach implementation for IE support:
- Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
- if (! Array.prototype.forEach) {
- Array.prototype.forEach = function (callback, thisarg) {
- var T, K;
- if (this= = null) {
- throw New TypeError ("This is anull or not defined");
- }
- var O = Object (this);
- var len = o.length >>> 0; //Hack to convert O.length to a UInt32
- if ({}.tostring.call (callback)! = "[Object Function]") {
- throw New TypeError (callback + "is not a function");
- }
- if (thisarg) {
- T = Thisarg;
- }
- k = 0;
- While (K < len) {
- var kvalue;
- if (k in O) {
- 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; The following two ways can be used:
1. If statement control
2. Return. (return True, false)
Return-like continue
The following example is the number of multiples of 2 in the array and 3;
[HTML]View Plaincopy
- <!--ADD by oscar999-->
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en">
- <HTML>
- <HEAD>
- <title> New Document </title>
- <META name= "Author" content="oscar999">
- </HEAD>
- <BODY>
- <script>
- if (! Array.prototype.forEach) {
- Array.prototype.forEach = function (callback, thisarg) {
- var T, K;
- if (this= = null) {
- throw new TypeError ("This is a null or not defined");
- }
- var O = Object (this);
- var len = o.length >>> 0;//Hack to convert O.length to a UInt32
- if ({}.tostring.call (callback)! = "[Object Function]") {
- throw new TypeError (callback + "is not a function");
- }
- if (Thisarg) {
- T = thisarg;
- }
- k = 0;
- while (K < len) {
- var Kvalue;
- if (k in O) {
- Kvalue = O[k];
- Callback.call (T, Kvalue, K, O);
- }
- k++;
- }
- };
- }
- var arryall = [];
- Arryall.push (1);
- Arryall.push (2);
- Arryall.push (3);
- Arryall.push (4);
- Arryall.push (5);
- var arryspecial = [];
- 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
[JavaScript]View Plaincopy
- arryall.foreach (function (e) {
- if (e%2==0)
- &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;
- Arryspecial.push (e);
- return;
- }
- if (e%3==0)
- {
- Arryspecial.push (e);
- RETURN;&NBSP;&NBSP;
- &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
- })
As to how to write the effect of similar break, there is no better way to find it.
There is a search, some say return false can be achieved, try, the effect and return and return ture is the same.
The Javascript array loops through the foreach