Js arrays are used to create, assign values, add, and remove elements based on subscripts (values or characters). This article will introduce them in detail, if you are interested, refer to 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 elements corresponding to the end are not included. If the end is omitted, all elements after the start are copied.
Array. concat (array1, array2); // concatenates multiple arrays into one 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
The Code is as follows:
Array. join (separator); // connects the array with separator.
No way to delete array elements according to subscript is 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.
In general, the subscript of an array is numeric, but there are also subscript of the numeric type.
For numeric processing, first write the following code, which is an extension of the array.
The Code is as follows:
Array. prototype. del = function (dx)
{
If (isNaN (dx) | dx> this. length) {return false ;}
This. splice (dx, 1 );
}
Second, the numeric type directly transmits the value parameter. For example, var arr = ["aa", "bb"]; arr. del (0 );
Next we will talk about the subscript of the struct type.
The Code is as follows:
Var arr = [].
Arr ["aa"] = 1;