ForEach of Javascript Array Traversal

Source: Internet
Author: User
1. js array looping. Array loop variables, the first thought of is the for (vari0; I & lt; count; I ++) method. In addition, you can also use the relatively simple forEach method 2. forEach function. The Array types of Firefox and Chrome both have forEach... SyntaxHighlig 1. js Array traversal. Array loop variable, first thought of IS for (var I = 0; I New Document Script var arryAll = []; arryAll. push (1); arryAll. push (2); arryAll. push (3); arryAll. push (4); arryAll. forEach (function (e) {alert (e) ;}) scriptHowever, the above Code cannot work normally in IE. Because the Array of IE does not have this method [javascript] alert (Array. prototype. forEach); after executing the above statement, we get "undefined", that is, in IE, Array 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] // 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 --> In the following example, return the multiples of 2 and 3 in the array. [html] New Document 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) ;}}) scriptUse return to achieve the above [javascript] arryAll. forEach (function (e) {if (e % 2 = 0) {www.2cto.com arrySpecial. push (e); return;} if (e % 3 = 0) {arrySpecial. push (e); return ;}}) as to how to write effects similar to break, no good method has been found. 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.
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.