This article mainly introduces the use of the foreach () method in JavaScript, which is the basic knowledge in the learning of JS, the need of friends can refer to the following
The ForEach () method of a JavaScript array invokes each element in the array.
Grammar
?
Use and introduction of the JavaScript forEach () traversal function, javascriptforeach
The forEach () function traverses the array from start to end. There are three parameters: array element, element index, and array (if a parameter is an array element, it is the value of the array.
Var data = [1, 2, 3, 4, 5, 6]; v
JavaScript has been born for more than 20 years. The method we have been using to loop an array is as follows:For (var index = 0; index Console. log (myArray [index]);}Since JavaScript5, we can use the built-in forEach method:MyArray. forEach (function (value ){Console. log (value );});It is a lot easier to write, but
The ForEach () method of a JavaScript array invokes each element in the array.Grammar
Array.foreach (callback[, Thisobject]);
The following are the details of the parameters:
Callback: Function tests each element of an array.
Thisobject: Object is used as the execution callback.
return value:
Returns the creation of an array.Compatibility:
This approach is a
Native JS uses foreach () and jquery to use each () to traverse the array, the difference of return false:
1. Use each () to traverse array A, as follows:
var a=[20,21,22,23,24];
$.each (A, function (index,val) {
console.log (' index= ' +index);
if (index==2) {return
false;
}
Console.log (' val= ' +val);
The results are as follows:
From the effect of
Use an array pointer to traverse the array, FOR/FOREACH to traverse the array, and foreach
1. traverse one-dimensional arrays with array pointers
2. FOR traversing two-dimensional arrays
$arr = array( array('a','b','c','d'), array('e','f','g','h'), array('i','j','k','l','m','n'), );for($i=0;$i
3. FORE
Use c: forEach to control 5 line breaks and foreach line breaks
When I was working on a project today, I encountered a problem. I needed to display images of uncertain numbers on the webpage (the images were retrieved from the database). I used the
Note: status. count indicates the number of iterations of the current iteration starting from 1.
A: How can struts
The following small series will introduce you to the array traversal forEach () and map () methods in JavaScript and the compatible writing method. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at it with xiaobian.
• Principle:
• Advanced browsers support the forEach Method
Syntax: Both
()method does not return execution results, but instead undefined . In other words, forEach() the original array is modified. Instead, map() the method gets a new array and returns.ExampleAn array is provided below, and if we want to double each of these elements, we can use map and forEach to achieve the goal.
Let arr = [5];
ForeachNot
1. js Array loop traversal.Array loop variables, the first thing to think about is the for (Var i=0;iIn addition, a simpler foreach method can be used2. The ForEach function.Both Firefox and Chrome's array types have a foreach function. Use the following:[HTML]View Plaincopy
>
HTML>
HEAD>
title> New Docu
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, firs
The array methods in the ECMA Script5 such as indexof (), ForEach (), map (), filter (), some () do not support ie6~8, but there is still a large majority of users in the country using Ie6~8, and the above array method is really useful. In the past, I would try not to use these methods for compatibility. But you can't lose a new one for the old one. Although jquery has been integrated with a lot of grammati
1. js Array loop traversal. array loop variable, the first thing to think about is the for (Var i=0;i
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:
But above, th
,arrayy) {...})But for NodeList to use the following notation.[].foreach.call (Lists,function (Valule.index.arrayy) {...})Why can ' t I use ForEach or map on a NodeList?NodeList is used very much like arrays and it would is tempting to use array.prototype methods on them. This is, however, impossible.JavaScript has a i
1.js Array loop traversal.
Array loop variable, the first thing to think about is the for (Var i=0;i
In addition, you can use a simpler foreach method
2.forEach function.
Both Firefox and Chrome's array types have foreach functions. Use the following:
But a
The two methods seem to have something to do with the name, but the difference between them is quite large in JavaScript.foreach () is used for the operation of an array, which executes the established function for each element in an array (not arrays cannot use the foreach () method). and $.each () is a method in jquery that executes the function that is developed for each matching element in the collectio
1. js Array loop traversal.Array loop variables, the first thing to think about is the for (Var i=0;iBoth Firefox and Chrome's array types have a foreach function. Use the following:But the above, the code in IE does not work properly.Because the array of IE does not have this methodThe implementation of the above sentence is "undefined", that is, in IE, the Array does not have a
+": "+ El ); } foreach ({A: 'A', B: 'B ', c: 'C'}, print); // 1: Stupid \ n 2: egg \ n 3: \ n 4: Seat \ n 5: right \ N 6: Ming foreach ("stupid motto", print); function person (name, age) { This. name = Name | ""; This. age = age | 0; }; person. prototype = new person; var Fred = new person ("jxl", 22); Fred. language = "Chinese"; // late binding // name: jxl \ n age: 22 \ n language: Chinese
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.