JavaScript's iterative method

Preface: About JS iterative method for array definition, I started in the "JavaScript Advanced Programming" in the study, then. . . I did not understand, then read the various chiefs of the blog, so a little understanding Diudiu. Here is my little opinion.

 

First, we need to know the iterative method an array of what are, enumerated are: every, some, filter, map, forEach, reduce.

Wherein every, some, filter, map, forEach five methods when an incoming function accepts three parameters, namely item (the item values ​​in the array), index (index entries), Array (array itself). In the following cases, I only passed two values, why. . . Less than (in fact, too lazy to write) 

The method will reduce the accepts four parameters, namely PREV (a front entry), CUR (current entry), index (index entries), Array (array of objects), any value of the function are returned as the first argument passed to the next iteration function is in the second start of the array.

 

Each 1.every ------- query whether the array will satisfy the conditions

var NUM = [1,2,3,4,5 ];
 var Number = num.every ( function (Item, index) {
       return (Item> 3);    // determines whether the passed value is greater than all 3 
}) 
console.log (Number The);    // false are not met shall be false

2.some ------- query each item in the array which satisfies the condition

var NUM = [1,2,3,4,5 ];
 var Number = num.some ( function (Item, index) {
       return (Item>. 3);    // determines which item is greater than the value passed in. 3 
}) 
Console .log (Number The);    // true as true can meet a

3.filter ------ screening eligible entries, to form a new array

var NUM = [1,2,3,4,5 ];
 var Number = num.filter ( function (Item, index) {
       return (Item>. 3);    // determines which item is greater than the value passed in. 3 
}) 
Console .log (Number);    // [4,5]

 Item 4.map ------ array by calculating the composition of the new array

var NUM = [1,2,3,4,5 ];
 var Number = num.map ( function (Item, index) {
       return Item *. 3;    // incoming values are multiplied. 3 
}) 
the console.log ( Number);    // [3,6,9,12,15]

5.forEach ------ passed each array

var num = [1,2,3,4,5];
var number = num.forEach(function(item,index){
      console.log(item);   // 1 2 3 4 5
})

    About forEach me a few more complaining, this method is no different for the cycle in nature. Mentioned above, each method will be passed three parameters, then I passed at the output of the first item, which is the output of each array. If the second pass, is the index of the array index value iteration cycle, if the third pass, the iterative loop is the entire array num.

Antecedent and array items be 6.reduce ------ accumulated value calculating

var NUM = [1,2,3,4,5 ];
 var Number = num.reduce ( function (PREV, CUR, index) {
       return PREV + CUR; 
}) 
the console.log (Number);    // . 1 + 2 3 + 4 + 5 + 15 = process item in the array is essentially cumulative operation

 

Guess you like

Origin www.cnblogs.com/Joyce7/p/11184757.html