1. Introduction to arrays
The element types in the array can be numeric, string, boolean, etc., or even an array.
2. Define an array
1. Define an array through the constructor of the array:
var arr=new Array(); var arr=new Array(size); var arr=new Array(element1,element2,...);
2. Define the array directly:
var arr=["String",true,13];
ps: Like Object, this writing method will not call the Array() constructor.
3. Array elements
1. Access array elements: through access operator[] such as arr[2];
2. Add array elements: directly assign values to array elements.
3. Delete array elements:
Once an array element is defined, it cannot be deleted. Using the delete operator can only delete the value of the array element, making the element value undefined, but it cannot delete an array element, and the elements in the array cannot be reduced.
4. Number of array elements: array length.
4. Array method
toString(): Convert array to string
join(): Concatenate array elements into strings, and automatically connect array elements with commas as connectors. 1,2,3
join(str): concatenate array elements into strings, and use str as a connector to connect array elements. join(-):1-2-3
Stack method:
push(): Add an element at the end of the array, and the return value is the length of the array after adding the element.
pop(): Remove the last element from the end of the array, reduce the length value of the array, and then return the removed element.
Queue method:
push(): Add an element at the end of the array, and the return value is the length of the array after adding the element.
shift(): Remove an element from the front end of the array, reduce the length value of the array, and then return the removed element. And move all remaining elements forward by 1 bit.
unshift(): Add an element from the front end of the array, and the return value is the length of the array after adding the element (IE browser does not support the return value, return undefined). an
Sort by:
reverse(): Invert
sort(): Sort by character encoding order
sort(order): The order parameter must be a function, and the function should have two parameters.
//Forward sortingfunction ascOrder(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } //Sorting in reverse orderfunction descOrder(value1, value2) { if (value1 < value2) { return 1; } else if (value1 > value2) { return -1; } else { return 0; } var box = [0,1,5,10,15]; (ascOrder); (descOrder);
Operation method:
concat(): Add elements and generate new array, the length of the original array does not change.
splice(): Delete, replace, or insert array elements.
splice(start,count,value,...):start:start position; count: number of elements
slice(start,end): Returns a part of the array.
toLocaleString(): Convert to local string.
The above is the relevant knowledge about the definition of JavaScript arrays and number operation techniques introduced to you by the editor. I hope it will be helpful to you!