I have been using js-related things, but I have seldom summarized them. today I have encountered some problems in operating js arrays and have a better understanding of js arrays.
1. create an array
The code is as follows:
Var array = new Array ();
Var array = new Array (size); // specify the length of the array
Var array = new Array (item1, item2 ...... ItemN); // create an array and assign values
2. value and value assignment
The code is as follows:
Var item = array [index]; // obtain the value of a specified element
Array [index] = value; // assign a value to a specified element
3. add new elements
The code is as follows:
Array. push (item1, item2 ...... ItemN); // add one or more elements to the array and return the length of the new array.
Array. unshift (item1, item2 ...... ItemN); // add one or more elements to the starting position of the array. The original element position is automatically removed and the length of the new array is returned.
Array. splice (start, delCount, item1, item2 ...... ItemN); // delete delCount elements from the start position and insert one or more new elements from the start position.
4. delete elements
The code is as follows:
Array. pop (); // delete the last element and return this element.
Array. shift (); // delete the first element. the array element is automatically moved forward and the deleted element is returned.
Array. splice (start, delCount); // delete delCount elements from the start position.
5. Merge and intercept arrays
The code is as follows:
Array. slice (start, end );
// Return part of the array in the form of an array. Note that the end element is not included. if the end element is omitted, all elements after start will be copied.
Array. concat (array1, array2 );
// Concatenates multiple arrays into an array
6. sort arrays
The code is as follows:
Array. reverse (); // array inversion
Array. sort (); // sorts the array and returns the array address.
7. convert an array to a string
Array. join (separator); // connects the array with separator.
The method for deleting array elements is not found if all the columns are listed! So I found some information and found the solution.
To delete an Array element, you need to extend the Array 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; };