How to Use foreach

Source: Internet
Author: User

[-]

  1. 1. js array looping
  2. 2 foreach Function
  3. 3. Make ie compatible with the foreach Method
  4. 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
  1. <! -- Add by oscar999 -->
  2. <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
  3. <HTML>
  4. <Head>
  5. <Title> new document </title>
  6. <Meta name = "author" content = "oscar999">
  7. </Head>
  8. <Body>
  9. <SCRIPT>
  10. VaR arryall = [];
  11. Arryall. Push (1 );
  12. Arryall. Push (2 );
  13. Arryall. Push (3 );
  14. Arryall. Push (4 );
  15. Arryall. foreach (function (e ){
  16. Alert (E );
  17. })
  18. </SCRIPT>
  19. </Body>
  20. </Html>

However, the above Code cannot work normally in IE.

Because the array of IE does not have this method

[JavaScript]View Plain Copy
  1. 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
  1. // Array. foreach implementation for IE support ..
  2. // Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
  3. If (! Array. Prototype. foreach ){
  4. Array. Prototype. foreach = function (callback, thisarg ){
  5. VaR T, K;
  6. If (this = NULL ){
  7. Throw new typeerror ("this is null or not defined ");
  8. }
  9. VaR o = Object (this );
  10. VaR Len = O. length> 0; // hack to convert O. length to a uint32
  11. If ({}. tostring. Call (callback )! = "[Object function]") {
  12. Throw new typeerror (callback + "is not a function ");
  13. }
  14. If (thisarg ){
  15. T = thisarg;
  16. }
  17. K = 0;
  18. While (k <Len ){
  19. VaR kvalue;
  20. If (K in O ){
  21. Kvalue = O [k];
  22. Callback. Call (T, kvalue, K, O );
  23. }
  24. K ++;
  25. }
  26. };
  27. }
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
  1. <! -- Add by oscar999 -->
  2. <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
  3. <HTML>
  4. <Head>
  5. <Title> new document </title>
  6. <Meta name = "author" content = "oscar999">
  7. </Head>
  8. <Body>
  9. <SCRIPT>
  10. If (! Array. Prototype. foreach ){
  11. Array. Prototype. foreach = function (callback, thisarg ){
  12. VaR T, K;
  13. If (this = NULL ){
  14. Throw new typeerror ("this is null or not defined ");
  15. }
  16. VaR o = Object (this );
  17. VaR Len = O. length> 0; // hack to convert O. length to a uint32
  18. If ({}. tostring. Call (callback )! = "[Object function]") {
  19. Throw new typeerror (callback + "is not a function ");
  20. }
  21. If (thisarg ){
  22. T = thisarg;
  23. }
  24. K = 0;
  25. While (k <Len ){
  26. VaR kvalue;
  27. If (K in O ){
  28. Kvalue = O [k];
  29. Callback. Call (T, kvalue, K, O );
  30. }
  31. K ++;
  32. }
  33. };
  34. }
  35. VaR arryall = [];
  36. Arryall. Push (1 );
  37. Arryall. Push (2 );
  38. Arryall. Push (3 );
  39. Arryall. Push (4 );
  40. Arryall. Push (5 );
  41. VaR arryspecial = [];
  42. Arryall. foreach (function (e ){
  43. If (E % 2 = 0)
  44. {
  45. Arryspecial. Push (E );
  46. } Else if (E % 3 = 0)
  47. {
  48. Arryspecial. Push (E );
  49. }
  50. })
  51. </SCRIPT>
  52. </Body>
  53. </Html>

Use return to achieve the above results [JavaScript]View Plain Copy
  1. Arryall. foreach (function (e ){
  2. If (E % 2 = 0)
  3. {
  4. Arryspecial. Push (E );
  5. Return;
  6. }
  7. If (E % 3 = 0)
  8. {
  9. Arryspecial. Push (E );
  10. Return;
  11. }
  12. })

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

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.