Well, unexpectedly can not single a picture, not enough 200 words ah not enough 200 words!In the JavaScript advanced programming, a very large number of methods are introduced in the category, among which there are 6 kinds of iterative methods, and these 6 methods have a very wide role in the actual project. I love using foreach and map, easy to use and efficient, not everything is for the
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
?
1
Array.foreach (callback[, Thiso
The following small series will bring you a forEach, $. each, and map method recommendation in JS. 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. forEach is the most basic new Array method in ecma5. it is traversal and loop. For example:
[1, 2, 3, 4]. forEach
Recently read some of the HTML5 and JS aspects of the book, benefited, because see more things, but there is no how to do the meditation, slowly come, perhaps recently a little nervous. Today I share with you the Foreach method of JavaScript (in fact, a method seen in the book "HTML5 Program Design").First, the standard format of the JavaScript foreach.Performs t
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
event Loop, but its operating mechanism is different from the browser environment.
Take a look at the schematic below (author @busyrich).
According to the above figure, the operating mechanism of Node.js is as follows.
(1) The V8 engine parses JavaScript scripts.(2) The parsed code calls the node API.(3) The LIBUV Library is responsible for the execution of the node API. It assigns different tasks to
remember them correctly. Similar to the following methods, such as $.map. var arr = [1,2,3,4]; var res= arr.foreach (function (Value,index,array) {Array[index] = value*4;}); Console.log (arr); The result is [4,8,12,16]console.log (res); Undefinedmap:map is the meaning of "mapping" similar to the use of ForEach, which is: [].map (function (Value,index,array) {//code}) var ary = [12,23,24,42,1]; var res = Ary.map (function (item,index,input) {return it
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 the operation can be seen, return is equivalent to the break in the
The Array.prototype.forEach () method lets each item of an array perform a given function once. -mdn
Suppose there was such a scene that you got such an array of
[{symbol: "XFX", price:240.22, volume:23432},{symbol: "TNZ", price:332.19, volume:234},{symbol: "Jxj", price:120.22, volume:5323},]
You need to create a new array for symbol, which is
["XFX", "TNZ", "JXJ"]Can generally be implemented in a For loop:
function Getstocksymbols (stocks
The for loop shows the number of loops, but the foreach does not know how to get the {code...} for loop shows the number of loops, but the foreach does not know how to get it.
Serial number Name Quantity Amount
The number of cycles is displayed as shown in the question, each time + 1 {$ Go
The ForEach () function iterates through the array from beginning to end. There are three parameters: the array element, the index of the element, the array itself (if a parameter is an array element, the value of the array).
var data=[1,2,3,4,5,6];
var sum=0;
Data.foreach (function (v) {//V is the value of the array 123456
sum+=v})
document.write (sum+ "
Note: foreach cannot terminate before all el
foreach is a method of manipulating an array in ES5, and the main function is to iterate through the array, for example:
12
vararr = [1,2,3,4];arr.forEach(alert);
Equivalent to:
1234
vararr = [1, 2, 3, 4];for(vark = 0, length = arr.length; k alert(array[k]);}
The function callback in the Foreach method has three parameters: the first argument
foreach is a method of manipulating an array in ES5, and the main function is to iterate through the array, for example: var arr = [1,2,3,4];arr.foreach (alert);Equivalent to:var arr = [1, 2, 3, 4];for (var k = 0, length = arr.length; k The function callback in the Foreach method has three parameters: the first argument is the array contents of the traversal, the second parameter is the corresponding array
. Map (FN) method1 var a=[2, 4, 6];2 a.map (function (d, I, arr) {3 return {x:i, y:d*i, z:arr};4});Output Result:1 [{x:0, y:0, Z:[2, 4, 6]}, 2 {x:1, Y:4, Z:[2, 4, 6]}, 3 {x:2, Y:12, Z:[2, 4, 6]}The d in the callback function is the value of the traversal array, and I is the index of the array, and the entire array is passed in the third parameter.Map casts the original array into a new array of the same length, and the value of each corresponding member in the new array is the value returned by
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.