Foreach of JavaScript Array Traversal

Source: Internet
Author: User
Tags javascript array
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:

<!--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);arryAll.forEach(function(e){alert(e);})</script></BODY></HTML>

However, the above Code cannot work normally in IE.

Because the array of IE does not have this method

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.
//Array.forEach implementation for IE support..//https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEachif (!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 UInt32if ({}.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/forEach4. 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;

<!--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 UInt32if ({}.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);}})</script></BODY></HTML>

Use return to achieve the above results

arryAll.forEach(function(e){if(e%2==0)    {arrySpecial.push(e);return;}if(e%3==0)    {    arrySpecial.push(e);return;}})

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.

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.