Iterative method: Type References

Every (), filter (), forEach (), Map (), some () the ECMAScript defined as an array. 5 5 iterative method.
 
Each method receives two parameters: Scope and object functions (optional) operation of the function to be run on each of the - value of this influence.
Passed to the function of these methods will be three parameters: the value of the array of items, and the position of the array of objects in the array itself.
 
Every () : Given a function, the function returns true if for each return true in the array each run.
filter () array to run on each item in the array for a given function, the function returns true return of items consisting of:
forEach () : an array each run a given function. This method has no return value.
the Map () : to run on each item in the array for a given function that returns the results of each array of function call.
some () : an array each run as a given function returns a true if any of the function, it returns true.
 
It is most similar to every () and some () , which are used to query the array item meets certain conditions.
For every (), the incoming function must return true for each, only this method returns true; otherwise, it returns
false。
And some () method is to simply pass the function returns true for an item in the array, it will return true.
 
eg:
var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every(function(item, index, array){
return (item > 2);
});
alert(everyResult); //false
// every (), passing the given entry as long as the function returns true greater than 2
 
var someResult = numbers.some(function(item, index, array){
return (item > 2);
});
alert(someResult); //true
// some (), a function of passing at least a greater than 2, returns true
 
 
filter () function , which uses the specified function comprises determining whether an item in the returned array, returns an array
 
eg: To returns an array of all values ​​greater than 2
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
});
alert(filterResult); //[3,4,5,4,3]
Created by calling the filter () method returns an array containing 3,4,5,4,3 and, because the function returns true they passed each.
 
map () returns an array , the results passed to the function and operation of each of which is corresponding to the entry in the array of the original array.
Create an array of items for one to one and another array contains.
 
eg: for each item in the array is multiplied by 2, and returns an array of these products
 
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
return item * 2;
});
alert(mapResult); //[2,4,6,8,10,8,6,4,2]
 
 
forEach () , is run on each item in the array passed in function. This method does not return value , essentially the same as an array using a for loop iteration.
 
 
 

Guess you like

Origin www.cnblogs.com/zmlAliIqsgu/p/12130271.html