The Javascript array loops through the foreach

Source: Internet
Author: User
Tags javascript array

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
  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>

But the above, the code in IE does not work properly.

Because the array of IE does not have this method

[JavaScript]View Plaincopy
    1. 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
  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 anull 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. }
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
  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 a 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 effect [JavaScript]View Plaincopy
    1. arryall.foreach (function (e) {  
    2.     if (e%2==0)   
    3. &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;
    4.          Arryspecial.push (e);   
    5.         return;  
    6.     }  
    7.     if (e%3==0)   
    8.     {       
    9.          Arryspecial.push (e);   
    10.          RETURN;&NBSP;&NBSP;
    11. &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
    12. })   

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

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.