Home >Web Front-end >HTML Tutorial >Detailed explanation of HTML array_html/css_WEB-ITnose
Array
1. Array declaration:
1. One-dimensional array
var x=new Array();//Empty array
var x = new Array(size); //Array of specified length
var x = new Array(element 0,element 1,...); //Specify initial value,,,, does not limit the data type stored
Also: var x =["Xiao Ming", "Xiao Hong"];//Indicates one-dimensional array array elements----Xiao Ming, Xiao Hong
3] means that the length of the array x is 3; = {'you', 'good', 'ah'};arr[1] = {'you', 'true', 'beautiful'};
arr[2] = {'you' , 'are' , 'full'};
//Traverse the two-dimensional array
for(var i=0; i
for(var j=0; i
}
Document.write('
}
2. The length and subscript of the array
1. var a = new Array(); define a null pointer
But a subscript that exceeds the length of the array means changing the length of the array
2. a.length=5 (adding numbers directly changes the length------can be lengthened or shortened)
But alert (a[6]) will display undefined
3. Commonly used methods for arrays
Methods
Function
EG
| concat | Concatenates two or more arrays and returns the result | |
var c=a.concat(b);
| Join | Convert all elements of the array Put a character into the element | |
Var a=new Array(''a,'b'); a.join('"!")
| pop | Remove and return the last element of the array | |
a.pop();
| Push | Adds one or more elements to the end of the array and returns the new length | |
Returns the length
| Reverse | Reverse the order in the array | |
Return new array after inversion
| Shift | Delete and return array The first element of | |
removes and returns the removed element
| Slice | Return selected elements from an existing array | |
var arr = ["George","John","Thomas","James" ,"Adrew","Martin"]; | document.write(arr.slice(2,-2)); //Thomas,James//Negative numbers count from the back to -2 (i.e. the third to last number) |
Sort() Sort the elements of the array | |
The default is to sort according to the order of character encoding Parameter writing comparison function |
| splice
Delete elements and add new elements to the array | |
1. splice(index deleted position Negative numbers count backwards, howmany quantity, new1, add from deleted position, new2...) |
|
tostring Convert the array to a string and return the result | |
is designed for strings and automatically adds , number
| Unshift | Adds one or more elements to the beginning of the array and returns the new length |
|