js array traversal some, foreach, map, filter, every comparison

From the original: http://www.cnblogs.com/jiebba/p/6514067.html
1. [...]. Some (ck) as a function ---- a true, that is true

Each element of the array to perform a ck function, returns true know an element right, directly returns true. If you return false, false is returned

Are there elements meet ck function checks the entire array.

var Result = [1,5,3,6] .some ((V, I) => (V> 10))       // all elements not satisfied, returns to false = Result 
var Result = [10,5,30, 60] .some ((V, I) => (V <10))       // there (s) satisfies a return result = true
2. [...]. Foreach (ck) function ---- cycle only

Each array element performs a ck function, foreach function can not jump out with a break

[50, 25, 49].forEach( (v,i) => console.log(v) );
//50     25      49
3. [...]. Map (ck) ---- function returns the return value set for each element

Each array element performs a function ck, and finally returns the set (array) after each element performs the function return value ck

var newArray = [50,30,40] .map ((V, I) => V / 10) // 10 divided by each element, and returns a new array newArray = [5,3,4]
4. [...]. Filter (ck) function returns true ---- give the set of elements

Each array element performs a function ck, ck finally returns after each executed function returns a collection of elements (arrays) for the true value of the element

var newArray = [50,2,60,4,53,15] .filter ((V, I) => (V> 10))    // Returns an array of elements 10 is greater than the new array newArray = [50,60, 53,15]
5. [...]. Every (ck) ---- a function of a false, false is returned                 

Each array element performs a function ck, ck until some element performs the function returns false, the direct returns false, if it returns all true, return true

var Result = [5,50,35,12,85] .every ((V, I) => V <51 is)   // return an (s) is equal to a greater than 51, result is returned to false = 
var Result = [ 5,50,35,12,85] .every ((V, I) => V <100)   // all greater than 51, return result = true

 6. [...]. Reduce (ck, init) ---- followed by the implementation ck (prv.next) 

 In order to perform an array of functions ck.

var result = [0,1,2,3]
restult.reduce((a,b)=> a+b,0)  // 返回 6

 

Guess you like

Origin www.cnblogs.com/junzhu-bye/p/11913374.html