Javascript Array loop traversal of foreach

Source: Internet
Author: User
Tags 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. The 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);

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

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

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

Because IE's array does not have this method

alert (Array.prototype.forEach);
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 to 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. (return True, false)
Return--> similar to 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);

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)
    {
		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.

There is a search, some say return false can be reached, tried, the effect and return and return ture is the same.



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.