ES5 array expansion

ES5 array of objects to add some methods commonly used 5:

1. Array.prototype.indexOf (value): get the first index value in the array

2. Array.prototype.lastIndexOf (value): value obtained last index in array

3. Array.prototype.forEach (function (item, index) {}): iterate

4. Array.prototype.map (function (item, index) {}): iterate returns a new array

5. Array.prototype.filter (function (item, index) {}): traversing a new filtered subarray

<! DOCTYPE HTML> 
<HTML lang = " EN " > 
<head> 
  <Meta charset = " UTF-. 8 " > 
  <title> 04_Array extended </ title> 
</ head> 
<body> 
<-! . 1 . The Array. prototype.indexOf (value): get the first index value in the array
 2 Array.prototype.lastIndexOf (value):. to give a final index value in the array
 . 3 . Array.prototype.forEach (function (Item , index) {}): iterate
 . 4 Array.prototype.map (function (Item, index) {}):. iterate return value after a new array, and processing returns
 . 5 . Array.prototype.filter (function ( item, index) {}): traversing a new sub-filter array, the return value of true conditions
 -> 
<script type="text/javascript
" >
   / * 
   Requirements: 
   1. The index of the first output 6. 
   2. The output of the last index 6 
   3. The output values of all elements and subscripts 
   4. generate a new array arr, than the requirements of each element 10 had large 
   5. the arr generating a new array, each element is greater than the return. 4 
   * / 

    var arr = [ . 1 , . 4 , . 6 , 2 , . 5 , . 6 ]; 
    the console.log (arr.indexOf ( . 6 )) ; // 2
     // Array.prototype.lastIndexOf (value): value obtained last index in the array of 
    the console.log (arr.lastIndexOf ( . 6 )); // . 5 

    // Array.prototype.forEach (function ( item, index) {}): iterate
    arr.forEach (function (Item, index) { 
        the console.log (Item, index); 
    }); 

    // Array.prototype.map (function (Item, index) {}): iterate returns a new array, return after processing value 
    var of arr1 = arr.map (function (Item, index) {
         return Item + 10 
    }); 
    the console.log (ARR, of arr1); 

    // Array.prototype.filter (function (Item, index) {} ): traversing a new sub-filter array, the return value of true condition 
    var arr2 is = arr.filter (function (Item, index) {
         return Item> . 4 
    }); 
    the console.log (ARR, arr2 is);


 </ Script > 
</ body> 
</ HTML>

 

Guess you like

Origin www.cnblogs.com/hack-ing/p/12005437.html