Handwritten JS code (a) a JavaScript array loops through the foreach

Source: Internet
Author: User
Tags javascript array

Note: Original address http://blog.csdn.net/oscar999/article/details/8671546

I am here to imitate the study!

1, the array of JS loop traversal

The traversal of the ① array is the first thought of the for () Loop statement

var arr = [' Summer ', ' I ', ' love ', ' You '];for (Var i=0, length=arr.length; i<length; i++) {      alert (arr[i]);}

② Second, a simpler approach to ForEach ()

Both FireFox and Chrome have the foreach () method in the array type, but the IE array type does not have a foreach () method.

var arr = [' Summer ', ' I ', ' love ', ' You '];/* can also define this array
var arr = [];
Arr.push (' Summer ');
Arr.push (' i ');
Arr.push (' love ');
Arr.push (' you ');
*/

Iterate through the array, calling the ForEach () method above the prototype of the array type directly Arr.foreach (function (e) { alert (e);})

2. The foreach () method for IE compatible arrays

IE in alert (Array.prototype.forEach)//undefined

The above code shows that the array type in IE does not have a foreach () method

Well, since the array type of IE does not have a foreach () method, we now add the foreach () 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 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++;    }        }; }

After the definition in IE, the array type has the foreach () method, which can be called.

var Arr=[1,2,3];arr.foreach (function (e) {alert (e)}); Pop-up in turn

3. The callback is passed in the ForEach (callback) method, callback is a callback function-the function that is called every time the traversal

For example, in fact, the callback function inside the foreach () is actually the code that we wrote in the for () loop body {}, which is actually an abstraction, and more clearly, we put the code that was previously written in the For () {} loop body {} into a function, It then passes in the foreach () method as a parameter: This is also a representation of the function as a parameter in JS.

Remove the number var Oldarr = [1,2,3,4,5,6,7,8];var NEWARR = [] that can be divisible by 2 or 3 in the array;  var  new =[]; this would be an error, the "new" is the key world in Javascriptoldarr.foreach (function (e) {     if (e%2 = = 0) {           newarr.push (e);           return; Note that this is not a break, or a continue;     else if (e%3 = = 0) {           newarr.push (e);           return;     }})

In fact, a look at the above to take out the array can be 2, 3 divisible by the number of logic is obviously not concise, the following improvements

var Oldarr = [1,2,3,4,5,6,7];var NEWARR = [];oldarr.foreach (function (e) {     if (e%2 ==0 | | e%3==0) {          newarr.push (e);     }})

Handwritten JS code (a) a 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.