An array of javascript
Learning Essentials:
An Introduction to arrays
Defining arrays
Array elements
Methods of arrays
First, the introduction of arrays
The element types in an array can be numeric, string, Boolean, and even an array.
Second, define the array
1. Define the array by the constructor of the array:
var arr=new Array ();
var arr=new Array (size);
var arr=new Array (Element1,element2,...);
2. Directly define the array:
var arr=["string", true,13];
PS: Like Object , this notation does not call the Array () constructor.
Third, array elements
1, access to array elements: through the access operator [] such as: arr[2];
2. Add an array element: assigns a value directly to the element.
3. Delete array elements:
Once an array element is defined, it cannot be deleted. You can only delete the value of an array element by using the delete operator, making the element value undefined, but you cannot delete an array element and not allow the elements in the array to be reduced.
4, the number of array elements: array length. Array.Length
Iv. Methods of arrays
ToString (): Converts an array to a string
Join (): Concatenate the array elements into a string and automatically concatenate the array elements with commas. The
Join (STR): Concatenate the array elements into a string and concatenate the array elements with STR as a connector. Join (-):
Stack method:
Push (): Adds an element at the end of the array and returns the length of the array after the element is added.
pop (): removes the last element from the end of the array, reducing the array's length value, and then returns the element that was removed.
Queue method:
Push (): Adds an element at the end of the array and returns the length of the array after the element is added.
shift (): Removes an element from the front of the array, reduces the length value of the array, and then returns the removed element. And move all remaining elements forward by 1 bits.
Unshift (): Adds an element from the front of the array and returns the length of the array after the element is added (ie does not support return values and returns undefined). An
Sort:
Reverse (): Invert
Sort (): Order by character encoding
Sort (order): The order parameter must be a function, and the function should have two parameters.
//Sort by positive orderfunctionAscorder (value1, value2) {if(Value1 <value2) { return-1;} Else if(Value1 >value2) { return1; } Else { return0;}//reverse OrderfunctionDescorder (value1, value2) {if(Value1 <value2) { return1;} Else if(Value1 >value2) { return-1; } Else { return0;}varbox = [0,1,5,10,15]; Box.sort (Ascorder); Box.sort (Descorder);
Operation Method:
Concat (): add element and generate new array, the original array length does not change.
Splice (): Deletes, replaces, or inserts an array element.
Splice (Start,count,value,...): start: Starting position; count: Number of elements
Slice (Start,end): Returns a portion of the array.
Tolocalstring (): Convert to local string.
Basic JavaScript Learning (10)