1. Array operation
1. Creation of arrays
The code copy is as follows:
var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array and specify the length, note that it is not the upper limit, it is the length
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]);//Create an array and assign a value
It should be noted that although the second method creates an array that specifies the length, in fact, the array is longer in all cases, that is, even if the length is specified, the element can still be stored outside the specified length. Note: The length will change accordingly.
2. Access to elements of array
The code copy is as follows:
var testGetArrValue=arrayObj[1]; //Get the element value of the array
arrayObj[1]= "This is a new value"; // Assign a new value to the array element
3. Adding array elements
The code copy is as follows:
arrayObj. push([item1 [item2 [. . . [itemN ]]]);// Add one or more new elements to the end of the array and return the new length of the array
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]);// Add one or more new elements to the array to start, and the elements in the array will automatically move backwards, returning the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//Insert one or more new elements into the specified position of the array, and the elements at the insertion position will automatically move backwards. ,return"".
4. Deletion of array elements
The code copy is as follows:
arrayObj.pop(); //Remove the last element and return the value of the element
arrayObj.shift(); //Remove the last element and return the element value, the elements in the array will automatically move forward.
arrayObj.splice(deletePos,deleteCount); //Delete the element of the specified number of deleteCount starting from the specified position deletePos, and return the removed element in the array form.
5. Intercept and merge of arrays
The code copy is as follows:
arrayObj.slice(start, [end]); //Return part of the array in the form of an array, note that the elements corresponding to end are not included. If end is omitted, all elements after start will be copied
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //Connect multiple arrays (can also be strings, or a mixture of arrays and strings) into an array, Return the connected new array
6. Copy of array
The code copy is as follows:
arrayObj.slice(0); //Return the copy array of the array, note that it is a new array, not pointing to
arrayObj.concat(); //Return the copy array of the array, note that it is a new array, not pointing to
7. Sort array elements
The code copy is as follows:
arrayObj.reverse(); //Reverse the element (the first one is ranked last, the last one is ranked first), and return the array address
arrayObj.sort(); //Sort array elements and return array address
8. Stringing of array elements
The code copy is as follows:
arrayObj.join(separator); //Returns a string, which joins each element value of the array together, separated by a separator.
toLocaleString , toString , valueOf: It can be regarded as a special usage of join, not often used
2. Three properties of array object
1. length attribute
The Length attribute represents the length of the array, that is, the number of elements in it. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1 respectively. Unlike most other languages, the length property of JavaScript arrays is mutable, which requires special attention. When the length attribute is set to be larger, the state of the entire array will not actually change, just the length attribute becomes larger; when the length attribute is set to be smaller than the original, the index of the element in the original array is greater than or equal to length All values are lost. Here is an example demonstrating changing the length attribute:
The code copy is as follows:
var arr=[12,23,5,3,25,98,76,54,56,76];
//Define an array containing 10 numbers
alert(arr.length); //Show the length of the array 10
arr.length=12; //Increase the length of the array
alert(arr.length); //Show the length of the array has changed to 12
alert(arr[8]); //Show the value of the 9th element, 56
arr.length=5; //Reduce the length of the array to 5, and elements with index equal to or exceeding 5 will be discarded
alert(arr[8]); //Show the 9th element has become "undefined"
arr.length=10; //Restore the array length to 10
alert(arr[8]); //Although the length is restored to 10, the 9th element cannot be retracted, showing "undefined"
From the above code we can clearly see the properties of the length attribute. But the length object can not only be explicitly set, it may also be implicitly modified. An undeclared variable can be used in JavaScript. Similarly, an undefined array element (refers to an element whose index exceeds or equals length). At this time, the value of the length attribute will be set to the value of the used element index. Add 1. For example, the following code:
The code copy is as follows:
var arr=[12,23,5,3,25,98,76,54,56,76];
alert(arr.length);
arr[15]=34;
alert(arr.length);
The code also first defines an array containing 10 numbers. Through the alert statement, it can be seen that its length is 10. Then, an element with an index of 15 is used, and the value is 15, that is, arr[15]=34. At this time, the length of the array is output using the alert statement, and the result is 16. Anyway, this is a surprising feature for developers accustomed to strongly typed programming. In fact, the initial length of an array created using the form of new Array() is 0. It is the operation of undefined elements that changes the length of the array.
From the above introduction, we can see that the length attribute is so magical that it can easily increase or decrease the capacity of the array. Therefore, an in-depth understanding of the length attribute will help to flexibly apply it during the development process.
2. Prototype attributes
Returns a reference to the object type prototype. The prototype attribute is shared by object.
The code copy is as follows:
objectName.prototype
The objectName parameter is the name of the object object.
Description: Use the prototype attribute to provide a set of basic functions of an object's class. A new instance of an object "inherits" the operation that gives the object prototype.
For array objects, use the following example to illustrate the purpose of the prototype attribute.
Add a method to the array object to return the maximum element value in the array. To accomplish this, declare a function, add it to Array.prototype, and use it.
The code copy is as follows:
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 this code is executed, y saves the maximum value in the array x, or 6.
3. constructor attribute
A function that represents the creation of an object.
object.constructor //object is the name of an object or function.
Description: The constructor property is a member of all objects with prototype. They include all JScript-inherited objects except the Global and Math objects. The constructor property holds a reference to a function that constructs a specific object instance.
For example:
The code copy is as follows:
x = new String("Hi");
if (x.constructor == String) // Process (condition is true).
or
The code copy is as follows:
function MyFunc {
// Function body.
}
y = new MyFunc;
if (y.constructor == MyFunc) // Process (condition is true).
3. Common functions and methods for array operations
toString(): Convert an array into a string
toLocaleString(): Convert an array into a string
join(): convert an array into a string connected with symbols
shift(): move an element in the header of the array out
unshift(): Insert an element at the head of the array
pop(): Delete an element from the end of the array
push(): Add an element to the end of the array
concat(): Add elements to the array
slice(): Returns the part of the array
reverse(): reverse sort the array in reverse
sort(): sort the array
splice(): Insert, delete or replace an array element