Method (2) of the array

8.slice: method returns a new array object, the object is a shallow copy of the original array is determined by the begin and end (including begin, not including the end). The same does not alter the original array

  = arr12 the let [24,. 3,. 6,. 8,. 7,. 9 ];
       // second parameter would have been copied to the end elements are omitted at the end of the array 
      the let res12, arr12.slice = (. 3 ); 
      the console.log (res12,) ; // [8,7,9]

9. splice (start [, deleteCount [, item1 [, item2 [, ...]]]]): This method has been before and slice could not tell, in fact, they have the biggest difference is that this method is to change the original the array will not re-open a space to change the length of the array.

     Start parameter specified modification start position (counting from 0). If you exceed the length of the array, from the end of the array begin adding content; if is negative, it means starting from the last one of an array of several (from -1 counts, this means is an inverse -n n-th element and the like
It is equivalent to array.length-n); if a negative absolute value greater than the length of the array, said first start position is 0.

deleteCount optional integer, indicates the number of array elements to be removed.

item1, item2, ... I want to add optional elements into an array, starting from the start position. If not specified, the splice () will only delete the array elements.

= arr13 the let [. 1,. 3,. 5,. 7,. 9,. 7, 2,. 6, 57 is,. 7 ];
       // add the first parameter represents a location index 
      arr13.splice (. 1, 0, "2"); // [1, "2", 3, 5, 7, 9, 7, 2, 6, 57, 7] 
      // number delete means to delete the second element parameter represents the end from the index to the array is omitted when delete all 
      arr13.splice (2); // [. 1, "2"] 
      // change 
      arr13.splice (0, 1, "ha" ); 
      the console.log (arr13); // [ "ha", "2 "]
// example familiar with these two methods: Start Remove from two elements 0, insert "Drum" 
      var myFish = [ "Angel", "Clown", "Mandarin", "Sturgeon" ]; 
      myFish.splice ( . 1 , 0, "Drum" ); 
      the console.log (myFish); // [ "Angel", "Drum", "Clown", "Mandarin", "Sturgeon"] 
      // start deleting an element from the bit. 3 
      var = myFish [ "Angel", "Clown", "Drum", "Mandarin", "Sturgeon" ]; 
      myFish.splice ( . 3,. 1 ); 
      the console.log (myFish); // [ "Angel", "Clown" , "drum", "sturgeon" ]

10. pop: no arguments, responsible for deleting the last element of the array, the original array change

 was arr14 = [1, 4, 5, 9 ]; 
      arr14.pop (); // [1, 4, 5] 
      console.log (arr14);

11. push: add a like end of the array element or elements arr.push (element1, ..., elementN)

var arr14 = [1, 4, 5, 9];
      arr14.push("3"); //[1, 4, 5,9,"3"]
      arr14.push("3", 2);
      console.log(arr14);

12.shift: Removes the first element of the array to change the original array

 var arr15 = [1, 4, 5, 9];
      arr15.shift(); //[4, 5, 9]
      console.log(arr15);

13.unshift: adding one or more elements of the array in the first place

  var arr16 = [6, 1, 4, 5, 9, 8];
      arr16.unshift("5", 3); //["5", 3, 6, 1, 4, 5, 9, 8]
      console.log(arr16);
14. fill: Method using a fixed value of all elements in the index array is filled from a starting index to termination. Excluding the terminating index.
      arr.fill(value[, start[, end]])
  Parameters: values ​​fill an array element value
        start the default starting index of 0
         end termination index default this.length
  var arr17 = [2, 3, 4];
      arr17.fill(6); //[6,6,6]
      arr17.fill("7", 0, 2); //["7", "7", 6]
      console.log(arr17);

15. flat: we must not familiar with this method, I'm doing a project when use it, take a look at its role method is very powerful but flat array compatibility impact

// parameter indicates the level of the flat 
      var arr18 = [2,. 6,. 8, [. 9, 10, 26 is ]]; 
      the let res18 = arr18.flat (. 1); // [2,. 6,. 8,. 9, 10, 26 is] 
      // Infinity may be of any depth flat array 
      // the let res18 = arr18.flat (Infinity) // [2,. 6,. 8,. 9, 10, 26 is] 
      the console.log (res18);

16.join: an array of all the methods (a class or an object array) elements into a string and returns a string. If the array has only one item, the item will be returned without the use of separator.

 // do not change the original array 
      var arr19 = [2, 2,. 6,. 3,. 4,. 5,. 6, "Y" ];
       var Res 19 = arr19.join ( ","); // 2,2,6,3 , 4,5,6, Y 
      var res20 arr19.join = ( ""); // 2263456y 
      the console.log (res20);

17. find: method returns an array of the first element satisfies the test functions provided. Otherwise it returns undefined. Note that the first

var arr21 = [ 
        {name: "LZ", Age: 21 is }, 
        {name: "LX", Age: 21 is }, 
        {Tine: "XX", AGEL: 23 is } 
      ]; 
      the let RES21 = arr21.find (ELE = > === 21 is ele.age ) 
      the console.log (RES21) // {name: "LZ", Age: 21 is the return element} 
      // want all qualified by the filter method can 
      let res22 = arr21.filter ( = ELE> === 21 is ele.age ) 
      the console.log (res22) // returns an array of [{...}, {...}]

18.findIndex: method returns an array index of the first element satisfies the test function provided. Otherwise, it returns -1. Note that the first

var arr23 = [
        { name: "lz", age: 21 },
        { name: "lx", age: 21 },
        { tine: "xx", ageL: 23 }
      ];
      let res23 = arr23.findIndex(ele => ele.age === 21)
      console.log(res23) //0 返回索引

19.indexOf: a first method returns the index to find a given element in the array, and if not, returns -1.

var arr24 = [1,
        { name: "lz", age: 21 },
        { name: "lx", age: 21 },
        { tine: "xx", ageL: 23 }
      ];
      let res24 = arr24.indexOf(1)
      console.log(res24) //0 返回索引

 

To practice more Europe!

 

Guess you like

Origin www.cnblogs.com/ZXH-null/p/12117863.html