foreach is the most basic of the new array methods, that is, traversal, loop. Let's look at how the array was traversed before.
Common traversal
var arr = [1,2,3,4,5]; for (var i = 0; i < arr.length; i++) { console.log (arr[i]); // 1,2,3,4,5}
To exclude null and undefined and non-existent element traversal
vararr = [1,undefined,NULL,, 5]; for(vari = 0; i < arr.length; i++){ if(!arr[i])Continue;//skipped null and undefined and non-existent elementsConsole.log (Arr[i]);//1,5 //equivalent to if(Arr[i]) {//skipped null and undefined and non-existent elementsConsole.log (Arr[i]); }}
Exclude undefined and non-existent element traversal
vararr = [1,undefined,NULL,, 5]; for(vari = 0; i < arr.length; i++){ if(Arr[i] = = =undefined)Continue;//skipping undefined and non-existent elementsConsole.log (Arr[i]);//1,null,5 //equivalent to if(Arr[i]!==undefined){//skipping undefined and non-existent elementsConsole.log (Arr[i]); } }
Skipping elements that do not exist, such as undefined elements without a value
var arr = [1,undefined,null, 5]; for (var i = 0; i < arr.length; i++) { ifincontinue; Console.log (Arr[i]); // 1,undefin,null,5 // equivalent to if inch arr) { console.log (arr[i]);} }
New method for traversing array elements in ECMAScript5, using the foreach () method
/** New method for traversing array elements in ECMAScript5, using the ForEach () method * @ Syntax: Arr.foreach (callback[, Thisarg]); * @ param callback//callback function * @ param THISARG//change callback function inside this point * @ syntax: Arr.foreach (function (value, index, array)); * @ par AM value//array of values * @ param index//array indices * @ param array//arrays themselves*///foreach Loopvararr = [1,2,3,4,5];arr.foreach (function(Value,index, array) {Console.log ("section"+ Index +"is the value of:"+ Value +", the array itself:"+array);});/*the value of logs No. 0 is: 1, the array itself: 1,2,3,4,5 1th value is: 2, the array itself: 1,2,3,4,5 2nd value is: 3, the array itself: 1,2,3,4,5 3rd value is: 4, the array itself: 1,2,3,4,5 4th value is: 5, the array itself: 1 , 2,3,4,5*/
foreach skips only elements that do not exist (no indexes, but can be accessed, such as arr[3], with a value of undefined)
var arr = [1,null,undefined, 5];arr.foreach (function(value,index , array) { Console.log (thevalue of "" is: " + value);}); /* the value of logs No. 0 is: 1 The 1th value is: null 2nd value is: Undefined 4th value is: 5 */
The second parameter of foreach changes the this point in the callback function
var arr = [1,2,3,4,5]; var arr2 = ["A","B","C","D","E"];arr.foreach ( function(value, index, array) { Console.log ("")is: " + value" ; Console.log (this); // the second parameter changes the callback function inside the This = ["A", "B", "C", "D", "E"];}, ARR2);
Polyfill
/** polyfill* @ ForEach () is a method in ECMAScript5*/if(!Array.prototype.forEach) {Array.prototype.forEach=function(callback,thisarg) {varT, K; if( This===NULL){ Throw NewTypeError ("This is a null or not defined") } varO =Object( This); varLen = o.length >>> 0; if(typeofCallback!=="function"){ Throw NewTypeError (callback +"is not a function"); } if(Arguments.length > 1) {T=Thisarg; } k= 0; while(K <Len) { varKvalue; if(kinchO) {Kvalue=O[k]; Callback.call (T,kvalue,k,o); } k++; } }}var len = o.length >>> 0; it is much better than var len = This.length | | 0; (parseint?), and it does not return unexpected values such as {}, [] when it encounters an unexpected this. 。 (IE 6+ Support) 1. All non-numeric values are converted to 02. All values greater than or equal to 0 are taken integer part//test type value >>> result
Array.prototype.forEach Array Traversal