However, the process of learning script Prototype.js found that some of the methods we rarely use, but some methods seem to be very classic, the script library is constantly increasing, the front of a ... Continue our JS primer, today to learn about array arrays.
First, let's look at its definition:
Copy Code code as follows:
var arrayobj = new Array ()
var arrayobj = new Array ([size])
var arrayobj = new Array ([element0[, element1[, [, ELEMENTN]]])
Where: Arrayobj is the variable name assigned to the Array object.
Size because the subscript of an array is zero-based, the subscript of the created element will be from zero to size-1.
Element0,..., ELEMENTN This creates an array of n + 1 with an element of n + 1. You must have more than one element when you use this syntax.
If you pass only one parameter to the Array's constructor, and the argument is a number, it must be an unsigned 32-bit integer (approximately 4 billion). The value becomes the size of the array. A run-time error occurs if the value is numeric, but is less than 0 or is not an integer.
If a single value is passed to the Array constructor and is not a number, the Length property is set to 1, and the unique element value becomes a single passed-in parameter.
Because the JS array is an analytic array, that is, although you can assign multiple elements to an array, only the elements that contain the data exist. This reduces the amount of memory used by the array.
The array object has three built-in properties: Constructor,length,prototype I will write a special article on constructor, Prototype,arguments and other object properties, here is not to say more. Here we look at some of the methods built into the array, which is important to us because it is often used.
Concat method: Joins two or more arrays and returns a new array. It is worth noting that (reference type) for object parameters copied from the array being connected to the new array. Copying still points to the same object, regardless of which of the new array and source array changes, will cause another change; (value type) copy only its value for a numeric value or string that is attached to the new array. A change in the value of an array does not affect the value in the other array. Example:
Copy Code code as follows:
1 function Concatarraydemo () {
2 var A, B, C, D;
3 A = new Array (1,2,3);
4 B = "JScript";
5 c = new Array ("VBScript");
6 d = A.concat (b, c);
7//return array [1, 2, 3, JScript, +, "VBScript"]
8 return (d);
9}
10
Join method: Converts an item in an array to a string with a specific delimiter and returns a comma with the default delimiter, as an example:
Copy Code code as follows:
1 function Joindemo () {
2 var a, B;
3 A = new Array (0,1,2,3,4);
4 B = A.join ("-");
5//back: "0-1-2-3-4"
6 return (b);
9 3
Pop method: Moves the last element in the divisor group and returns the element. Returns undefined if the array is empty.
Push methods: These elements are added in the order in which the new elements appear, and if one of the arguments is an array, it is added to the array as a single element. If you want to merge elements from two or more arrays, use the Concat method.
The reverse method reverses the position of an element in an Array object. During execution, this method does not create a new Array object. If the array is not contiguous, the reverse method creates an element in the array to populate the interval in the array. The value of all the elements created by this is undefined.
Copy Code code as follows:
1 function Reversedemo () {
2 var a, l; Declare a variable.
3 A = new Array (0,1,2,3,4); Creates an array and assigns a value.
4 L = a.reverse (); Reverses the contents of an array.
5//return: l=[4,3,2,1,0]
6 return (L); Returns an array of results.
9 3
The shift method moves the first element in the divisor group and returns the element.
The slice method returns an Array object that contains the specified portion of the arrayobj. The slice method is copied to the element specified by end, but does not include the element. If start is negative, it is the length + start processing, where length is the size of the array. If end is negative, it is treated as length + end where length is the length of the array. If End is omitted, the slice method is copied to the ending of arrayobj. If End appears before start, no elements are copied into the new array. Example:
In addition to the last element, all elements in the myarray are copied to the NewArray:
NewArray = Myarray.slice (0,-1)
The sort method sorts the array object appropriately, and does not create a new array object during execution. If you provide a function for the sortfunction parameter, the function must return one of the following values:
· Negative value if the first argument passed is smaller than the second argument.
· 0 if two parameters are equal.
· Positive value if the first argument is larger than the second argument.
Copy Code code as follows:
1 function Sortdemo () {
2 var a, l; Declare a variable.
3 A = new Array ("X", "Y", "D", "Z", "V", "M", "R");
4 L = A.sort (); The sort array.
5 return (L); Returns the sorted array.
6}
The splice method modifies arrayobj by removing the specified number of elements starting from the start position and inserting new elements. The return value is a new Array object that consists of the removed element. The format is as follows:
Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]
The toLocaleString method is explained in the Date object, and generally this method is returned to the user and is not evaluated as code.
The Unshift method inserts these elements into the beginning of an array, so these elements appear in the order in the sequence of arguments in the array. The format is as follows:
Arrayobj.unshift ([item1[, item2 [, ...] [, Itemn]]]
The elements of the valueof method &tostring () array are converted to strings, separated by commas, and concatenated together. The operation is the same as the array.tostring and Array.join methods.