JavaScript array loop Traversal of the foreach Detailed _ Basics

Source: Internet
Author: User
Tags anonymous javascript array


1.js Array loop traversal.



Array loop variable, the first thing to think about is the for (Var i=0;i<count;i++) way.



In addition, you can use a simpler foreach method



2.forEach function.



Both Firefox and Chrome's array types have foreach functions. Use the following:


<!--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); 

Anonymous mode
Arryall.foreach (function (e) { 
  alert (e); 
}) 

function T1 (ARG) {alert (arg);}
Non-anonymous way
Arryall.foreach (t1,arryall);

</script> 
</BODY> 
</HTML>


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



Because IE's array does not have this method


 
 


The execution of the above sentence is "undefined", which means that the Array in IE does not have a foreach method.



3. Make IE compatible with foreach method



Since IE's array does not have a foreach method, we will manually add this prototype method to it.


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 Typee Rror (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 refer to:
Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach



4. How do I jump out of the loop?



Js This kind of condition foreach cannot use continue, break; You can use the following two ways:



1. If statement control



2. Return statement control (return TRUE or return false)



In fact return similar to the role of continue



The following example is the number of multiples of 2 in an array and a multiple of 3;


<!--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);
arryAll.push(6); 
arryAll.push(7); 
 
 
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


Arryall.foreach (function (e) { 
  if (e%2==0) { 
  www.jb51.net
    Arryspecial.push (e); 
    return; 
  } 
  if (e%3==0) 
  {   
    arryspecial.push (e); 
    return; 
  } 
})


As for how to write the effect of similar break, there is no better way to find out.



Personal view: In either Java or C # syntax, foreach is the traversal of all values



There is a search, some say return false can be reached, tried, return false effect and return is the same, and return ture is the same.
The following test code was added by myself.


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;
});


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


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.