Advanced browsers (including IE9 and above) support the map and foreach methods to loop through the array, with the same usage, but there are some differences that must be known in order to correctly select in the project
Principle:
Advanced browsers support the Foreach methodSyntax: both foreach and
The use of java8 ForEach in map and list
the original use
map
ForEach How to use
Items.foreach ((k,v)->system.out.println ("key:" + K +); Value: "+ V");
Output
key:a value:10
key:b value:20
key:c value:30
key:d value:40 key:e valu
E:50
key:f value:60
Items.foreach ((k,v)->{
System.out.println ("Item:" + k
This address:http://www.cnblogs.com/veinyin/p/8794677.htmlBoth foreach and map are iterative methods of arrays, and each item of an array executes a given function without altering the original array.The difference is that foreach has no return value, and map returns a new array of data after each execution.So when cho
Let's first look at the similarities between the two.var arr = [' A ', ' B ', ' C ', ' d '];arr.foreach (Item,index,arr) {//item represents each item in the array, index identifies the subscript for the current item, Arr represents the current array console.log (item); Console.log (index); Console.log (arr); Console.log (this);},123); Here the 123 parameter, which represents the function of this point, can be written without writing, if not written, then this point windowarr.map (fun
The main use of foreach is in the build in condition, which can iterate a collection in an SQL statement. The properties of the Foreach element are mainly item,index,collection,open,separator,close. The item represents the alias of each element in the collection when it iterates, and index specifies a name that represents the position at which each iteration occurs during the iteration, and open indicates w
", "6");//return NULL The second parameter is 0Console.log (num);//[3,5,6,4]varChange=num.splice (1, 1, "7", "8");//[5]Console.log (num);//[3,7,8,6,4]Note: Slice affects the original array, splice does not affect the original array Next, Es5 added in the Map,foreach,filter,every,some and so onThese iterative methods contain two parameters: the first method function to operate on an array object, and the sec
,map,foreach,reduce will not change the original array.
Methods of arrays
Role
Traversal array foreach ()
Each element of an array executes a callback function once
Filter ()
Detects a numeric element and returns an array of all elements that match the condition.
foreach is the most basic of the new array method in ECMA5, that is, traversal, loop. For example, the following example:[1, 2, 3, 4].foreach (alert);is equivalent to the following for loop1 var array = [1, 2, 3, 4]; 2 for (var k = 0, length = array.length; k ) {3 alert (array[k]); 4 }Array in ES5 new method, the parameters are function type, the default is the parameter, the function callback in the
First, the definitionforeach (): Iterates through the array, invoking the specified function for each element.Map (): Passes each element of the called array to the specified function and returns an array that contains the return value of the function.The passed function is the first parameter of the foreach ()/Map (), which has three parameters: the element of the array + the index of the element + the arr
1.map ()DescriptionInstance:2.forEach ()DescriptionInstance:3.split ()DescriptionInstance:4.slice ()DescriptionThe first parameter represents the start position, the second parameter represents the next position of the end position, the length of the truncated string is the difference between the second parameter and the first parameter, and if the value of the parameter is negative, the value is added to a
1.forEach ()
foreach is a method of manipulating arrays in ES5, and the main function is to traverse an array. The function callback in the Foreach method has three parameters: the first argument is the array content of the traversal, the second parameter is the corresponding array index, and the third argument is the array itself
So:
[].
function a() { var arr = new Array (1000000); For (var i = 0; I + +) { Arr[i] = i;}var start1 = new Date (). GetTime (); For (var i = 0; I + +) { Arr[i] = i+1; }var stop1 = new Date (). GetTime (); console.info (Stop1-start1); var start2 = new Date (). GetTime (); Arr.ForEach(function(value, index, array) { Arr[i] = i+1; });var stop2 = new Date (). GetTime (); console.info (stop2-start2); var start3 = new Date (). GetTime (); Arr.map(function(index, value, array) { Arr[i] =
corresponding method for each element
var arr = [1,2,3,4,5,6,7,8];
Uses the usual "for" loop to iterate
for (Var i= 0, L = arr.length; i
foreach is used to replace the For loop
4) Map ()
Map () After each element of the array is manipulated (mapped), a new array is returned,
Do not use map
var
1. [...]. True if the Some (CK) function----One of the truePerforms a CK function once for each element in the array, and returns True if an element returns TRUE. False if both return falseChecks whether the entire array has elements that satisfy the CK function.var result = [1,5,3,6].some ((v,i) = (v>10))//All elements are not satisfied, return result = Falsevar result = [10,5,30,60].some ((v,i) = (v2.[...]. The foreach (CK) function----loop.Each arr
Functional programming Sort Map ForEach .... JQuery ()The sort method of an array passing in an anonymous function is functional programmingIE9 above browsers support the map methodThe map method returns a new arrayvar t = [1,3,9,10,12]for (var i in t) {Console.log (T[i])}Each element in an array isvar m = t.map (funct
1. [...]. True if the Some (CK) function----One of the truePerforms a CK function once for each element in the array, and returns True if an element returns TRUE. False if both return falseChecks whether the entire array has elements that satisfy the CK function.1. var result = [1,5,3,6].some ( (v,i) = ( v>10)) // all elements are not satisfied, return result = False2,var result = [10,5,30,60].some ( v,i) = (v/ / have one (multiple) satisfied, return result = True 2. [...]. The
) res6:list[string] = List (Kraps, Poodah, Esabh)Flatmap function:The "FlatMap" function has half the same function as the map function, but there is a requirement that the returned value of the incoming function be a list (should be seq) after processing, and if the result is not a list (seq), an error occurs. In other words, the function passed in is required-the return value is a SEQ line. Thus, after each element is processed, a list is returned,
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.