I am not very familiar with JS. I recently used Array to sort it out. Reading others' articles and adding my own lab operations are not completely original.
Three attributes of an array object
1. length attribute
The Length attribute indicates the Length of the array, that is, the number of elements. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1. Unlike most other languages, the length attribute of the JavaScript array is variable, which requires special attention. When the length attribute is set to a greater value, the status of the entire array does not actually change, except that the length attribute becomes larger. When the length attribute is set to an hour later than the original value, all the values of the elements whose indexes are greater than or equal to the length in the original array are lost. The following is an example of changing the length attribute:
Var arr = [,]; // defines an array containing 10 numbers
Alert (arr. length); // display the length of the array by 10
Arr. length = 12; // increase the length of the array.
Alert (arr. length); // display that the length of the array has changed to 12
Alert (arr [8]); // displays the value of the 9th elements, 56
Arr. length = 5; // reduce the length of the array to 5. elements whose index is equal to or greater than 5 are discarded.
Alert (arr [8]); // display that 9th elements have changed to "undefined"
Arr. length = 10; // restore the array length to 10
Alert (arr [8]); // although the length is restored to 10, 9th elements cannot be recovered, and "undefined" is displayed"
From the code above, we can clearly see the nature of the length attribute. However, the length object can be explicitly set and may be implicitly modified. JavaScript can use an undeclared variable, or an undefined array element (an element whose index exceeds or is equal to length, the value of the length attribute is set to the value of the used element index plus 1. For example, the following code:
Var arr = [,]; // defines an array containing 10 numbers
Alert (arr. length); // display 10
Arr [15] = 34;
Alert (arr. length); // display 16
The Code also defines an array containing 10 numbers. The alert statement shows that the length is 10. Then, an element with an index of 15 is used and assigned a value of 15, that is, arr [15] = 34. Then, the array length is output using the alert statement, and the result is 16. In any case, this is a surprising feature for developers who are used to strong-type programming. In fact, the initial length of an Array created in the form of new Array () is 0, and the length of the Array changes only when no element is defined.
From the above introduction, we can see that the length attribute is so magical that it can be used to conveniently increase or decrease the array capacity. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.
2. prototype attributes
Returns a reference to an object type prototype. The prototype attribute is common to objects.
ObjectName. prototype
The objectName parameter is the name of the object.
Note: The prototype attribute is used to provide a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype.
The following example describes the purpose of the prototype attribute for array objects.
Add a method to the array object to return the maximum element value in the array. To do this, declare a function, add it to array. prototype, and use it.
Function array_max ()
{
VaR I, max = This [0];
For (I = 1; I <this. length; I ++)
{
If (max <this [I])
Max = This [I];
}
Return Max;
}
Array. Prototype. max = array_max;
VaR x = new array (1, 2, 3, 4, 5, 6 );
Var y = x. Max ();
After the code is executed, y saves the maximum value in array X, or 6.
3. constructor attributes
The function that creates an object.
Object. constructor // object is the name of an object or function.
Note: The constructor attribute is a member of all objects with prototype. They include all inherent JScript objects except global and math objects. The constructor attribute stores references to the functions used to construct a specific object instance.
For example:
X = new String ("Hi ");
If (x. constructor = String) // process (the condition is true ).
// Or
Function MyFunc {
// Function body.
}
Y = new MyFunc;
If (y. constructor = MyFunc) // process (the condition is true ).
For arrays:
Y = new Array ();
8 categories of array objects and multiple methods
1. Create an array
Var arrayObj = new Array (); // create a default Array with a length of 0
Var arrayObj = new Array (size); // create an Array of the size length. Note that the length of the Array is variable, so it is not the upper limit, but the length.
Var arrayObj = new Array (item1, item2 ,.....
); // Create an array and assign the Initial Value
It should be noted that, although the second method creates an array with a specified length, the array actually gets longer in all cases, that is, even if the length is 5, you can still store elements outside the specified length. Note: The length will change accordingly.
2. Access to array elements
Var ArrayItemValue = arrayObj [1]; // gets the element value of the array.
ArrayObj [1] = "assign a new value"; // assign a new value to the array element
3. Add array elements
ArrayObj. push (item1, item2,...); // Add the parameter to the end of the array and return the new length of the array.
Example:
Var a = [1, 2, 3, 4, 5];
A. push (6, 7); // The result of a is: [1, 2, 3, 4, 5, 6, 7].
ArrayObj. unshift (item1, item2,...); // Add the parameter to the array to start. The elements in the array are automatically removed and the new length of the array is returned.
Example:
Var a = [1, 2, 3, 4, 5];
A. unshift (-2,-1); // The result of a is: [-2,-,].
ArrayObj. splice (start, deleteCount, item1, item2 ,...); // Delete the deleteCount element from the subscript start and insert item1, item2 ,..., the insert position and the elements after it are automatically moved back. The deleted items are returned.
Example:
Var a = [1, 2, 3, 4, 5];
Var B = a. splice (, 9); // The result of a is: [,]. The result of B is 5: [].
Note:
Var B = a. splice (0, 1); // equivalent to shift ()
A. splice (0, 0,-2,-1); var B = a. length; // equivalent to the unshift () method
Var B = a. splice (a. length-1, 1); // equivalent to the pop () method
A. splice (a. length, 7); // equivalent to the push () method
4. Deletion of array elements
ArrayObj. pop (); // Delete the last entry of the original array;
Example:
Var a = [1, 2, 3, 4, 5];
A. pop (); // The result of a is: [1, 2, 4]
ArrayObj. shift (); // removes the first element and returns the element value. The elements in the array are automatically moved forward.
Example:
Var a = [1, 2, 3, 4, 5];
A. shift (); // The result of a is: [2, 3, 4, 5]
ArrayObj. splice (start, deleteCount); // Delete the deleteCount elements from the subscript start. The method returns the removed elements in an array.
Example:
Var a = [1, 2, 3, 4, 5];
Var B = a. shift (); // The result of a is [, 5], and B is [].
5. truncate and merge Arrays
ArrayObj. slice (start [, end]); // returns a new array consisting of items starting from the start subscript to the end-1 subscript in the original array, if the end parameter is left blank, all elements after the start subscript are returned.
Example:
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // The result of a is [, 5]. The result of B is [2, 3].
Var B = a. slice (1); // The result of a is: [1, 2, 3, 4, 5]. The result of B is [2, 3, 4, 5].
ArrayObj. concat (item1, item2,...); // returns a new array consisting of parameters added to the original array, while the original array remains unchanged.
Example:
VaR A = [1, 2, 3, 4, 5];
VaR B = A. Concat (6, 7); // The result of A is [1, 2, 3, 4, 5]. The result of B is [1, 2, 3, 4, 5, 7].
6. Copy an array
Arrayobj. Slice (0); // returns the copy array of the array. Note that it is a new array, not pointing
Arrayobj. Concat (); // returns the copy array of the array. Note that it is a new array, not pointing
7. Sorting of array elements
Arrayobj. Reverse (); // returns the array address in reverse order.
Example:
VaR A = [1, 2, 3, 4, 5];
VaR B = A. Reverse (); // The result of A is: [5, 4, 3, 2, 1]. The result of B is: [5, 4, 3, 2, 1].
// Note that the returned address is an array address, which is not a copy of the array. If the original array changes after the reverse order, the reverse order array also changes randomly.
// Perform the following operations on this
A. Pop (); // A: [5, 4, 3, 2]; B: [5, 4, 3, 2]
// Or
B. Pop (); // The result of A is: [5, 4, 3, 2]. The result of B is: [5, 4, 3, 2].
Arrayobj. Sort ([orderfunction]); // sorts by specified parameter array elements and returns the sorted array address.
Example:
// Continue sort () in the arrayobj. Reverse () Example ()
A. Sort (); // The result of A is [2, 3, 4, 5]. The result of B is [2, 3, 4, 5].
Note that you can sort data by the method specified by the orderfunction parameter in an array.
8. stringized array elements
ArrayObj. join (separator); // Concatenates the elements of the array to form a string. If this parameter is omitted, the separator is separated by commas.
Var a = [1, 2, 3, 4, 5];
Var B =. join ("|"); // The result of a is [1, 2, 3, 4, 5]. The result of B is: "1 | 2 | 3 | 4 | 5"
ToLocaleString, toString, valueOf: can be considered as a special use of join, not commonly used.
Reprinted address
Http://www.cnblogs.com/lynnlin/archive/2009/03/06/1404830.html