The js array deletes elements by subscript

  • 2020-05-27 04:24:35
  • OfStack

1. Create an array


var array = new Array();
var array = new Array(size);// Specifies the length of the array
var array = new Array(item1,item2 ... itemN);// Create the array and assign the value

2. Value and assignment


var item = array[index];// Gets the value of the specified element
array[index] = value;// Assigns a value to the specified element

3. Add new elements


array.push(item1,item2 ... itemN);// will 1 Two or more elements are added to an array, returning the length of the new array
array.unshift(item1,item2 ... itemN);// will 1 Four or more elements are added to the starting position of the array, and the original element position is automatically moved back and returned   The length of the new array
array.splice(start,delCount,item1,item2 ... itemN);// from start The position begins to be deleted backward delCount And then from start The position begins to insert 1 Two or more new elements

4. Delete elements


array.pop();// Delete the last 1 And returns that element
array.shift();// Delete the first 1 The array element position is automatically moved forward to return the deleted element
array.splice(start,delCount);// from start The position begins to be deleted backward delCount An element

5. Combination and interception of arrays


array.slice(start,end);
// Returns an array as an array 1 Partial, not included end The corresponding element, if omitted end Will be copied start Everything after that
array.concat(array1,array2);
// Splice multiple arrays together 1 An array

6. Sorting arrays


array.reverse();// An array of reverse
array.sort();// Array sort, return array address

7. Array to string

array. join (separator); // concatenates array causes with separator

Column so all is not found to delete the array element method! Then looked up 1 some data to find the solution.
Deleting array elements requires extending the Array prototype prototype.


Array.prototype.del=function(index){
    if(isNaN(index)||index>=this.length){
      return false;
    }
    for(var i=0,n=0;i
      if(this[i]!=this[index]){
        this[n++]=this[i];
      }
    }
    this.length-=1;
  };


Related articles: