[-]
- 1. js array looping
- 2 foreach Function
- 3. Make ie compatible with the foreach Method
- 4. How to jump out of a loop
1. js array looping. Array loop variables, the first thought of is for (VAR I = 0; I <count; I ++.
In addition, you can also use a relatively simple foreach method.
2. foreach function. Both the array type of Firefox and chrome have foreach functions. Use:
[HTML]View Plain Copy
- <! -- 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>
However, the above Code cannot work normally in IE.
Because the array of IE does not have this method
[JavaScript]View Plain Copy
- Alert (array. Prototype. foreach );
Run the preceding statement to get "undefined", that is, the array in IE does not have the foreach method.
3. Make ie compatible with the foreach method. Since the array of IE is not the foreach method, we will manually add this prototype method to it.
[JavaScript]View Plain Copy
- // 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 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 ++;
- }
- };
- }
For details, refer:
Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach 4. How to jump out of the loop? In this case, JS foreach cannot use continue or break. The following two methods can be used:
1. If statement Control
2. Return. (return true, false)
Return --> similar to continue
The following example shows the number of multiples of 2 and 3 in the array;
[HTML]View Plain Copy
- <! -- 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 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 results
[JavaScript]View Plain Copy
- Arryall. foreach (function (e ){
- If (E % 2 = 0)
- {
- Arryspecial. Push (E );
- Return;
- }
- If (E % 3 = 0)
- {
- Arryspecial. Push (E );
- Return;
- }
- })
As for how to write performance similar to break, there is no better way yet.
If you search for the result, some say that return false can be achieved. If you try it, the result is the same as return and return true.
How to Use foreach