Basic usage of js arrays and array removal of elements based on subscript of values or characters

  • 2020-03-26 21:27:31
  • 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 an array and assign values

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

Add new elements
array.push(item1,item2 ... itemN);//Adds one or more elements to an array, returning the length of the new array
array.unshift(item1,item2 ... itemN);//When one or more elements are added to the starting position of the array, the original element position is automatically moved back to return the length of the new array
array.splice(start,delCount,item1,item2 ... itemN);//Delete the delCount elements backward from the start position, and then insert one or more new elements from the start position

4. Delete elements
array.pop();//Delete the last element and return it
array.shift();//When the first element is deleted, the array element position automatically moves forward, returning the deleted element
array.splice(start,delCount);//DelCount elements are deleted backward from the start position

5. Combination and interception of arrays
array.slice(start,end);//Returns a portion of the array in the form of an array. Note that the element corresponding to the end is not included
array.concat(array1,array2);//Splice multiple arrays into one array

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

7. Array to string
array.join(separator);//Connect the array reason with separator

Column so all is not found by the index to delete array elements! So I looked up some data and found a solution.
Deleting Array elements requires expanding the Array prototype.
Most arrays have numeric subscripts, but there are also character subscripts
Numerical processing, the first to write the following code, is an extension of the array
Array.prototype.del = function(dx) 
if(isNaN(dx)||dx>this.length){return false;} 

Secondly, the numerical type can directly pass the numerical parameters. For example, var arr = ["aa","bb"]; Arr. Del (0);
Let's talk about subscripts for character types
var arr = []. 
arr["aa"] = 1; 

Related articles: