splice slice string () and arrays ()

1. string taken: slice ()

Function: arrayObject.slice (start, end)

  start: Required. Choose where to start working. If it is negative, then it provides an array of tail begin to run from the location. That is, the last element refers -1, -2 means penultimate element, and so on.

  end: Optional. Where the provisions of the end of selection. This parameter is an array index at the end of an array of fragments. If this parameter is not specified, then sliced ​​array contains all of the elements from the start to the end of the array. If this parameter is negative, then it is prescribed begin to run from the end of the array elements.

  It returns a new array containing elements from start to end (not including the element) in the arrayobject.

example:

var STR = 'ahji3o3s4e6p8a0sdewqdasj' 
Alert (str.slice ( 2,5))    // results ji3

2. Remove the specified array elements: splice ()

Examples: a Deletion 2 (frontmost)

var ARR = [1,2,2,1 ];
 for (the let I = 0; I <arr.length; I ++ ) {
     IF (ARR [I] == 2 ) { 
      arr.splice (I, . 1 );
       BREAK ; // this line becomes i--, all are removed 2 
    } 
} 
the console.log (ARR); // [. 1, 2,. 1]

 

Guess you like

Origin www.cnblogs.com/yangai/p/11328905.html