Detailed Explanation of JavaScript Array Operation

  • 2021-07-16 01:32:16
  • OfStack

1. Array creation


var arrayObj = new Array();   // Create 1 Array of numbers 
var arrayObj = new Array([size]);   // Create 1 Array and specify the length, note that it is not the upper limit, but the length 
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]);   // Create 1 Arrays and assign values 

It should be noted that although the length of the array created by the second method is specified, in fact, the array is variable in all cases, which means that even if the length is specified as 5, the element can still be stored outside the specified length. Note that the length will change accordingly.

2. Access to elements of an array


var testGetArrValue=arrayObj[1]; // Gets the element values of the array 
arrayObj[1]= " This is the new value "; // Assign a new value to an array element 

3. Adding Array Elements

Code


arrayObj. push([item1 [item2 [. . . [itemN ]]]]);//  Will 1 One or more new elements are added to the end of the array and the new length of the array is returned 
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);//  Will 1 When new elements are added to the array, the elements in the array automatically move backward and return the new length of the array 
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);// Will 1 One or more new elements are inserted into the specified position of the array, and the elements at the inserted position are automatically moved backward and return "" . 

4. Deletion of array elements


arrayObj.pop(); // Remove the last 1 Elements and returns the value of that element 
arrayObj.shift(); // Remove the foremost 1 Elements and returns the value of that element, and the elements in the array are automatically moved forward 
arrayObj.splice(deletePos,deleteCount); // Delete from the specified location deletePos The specified quantity to start deleteCount Returns the removed elements as an array 

5. Interception and merging of arrays


arrayObj.slice(start, [end]); // Returns an array as an array 1 Part, note that it does not include  end  Corresponding element, if omitted  end  Will copy  start  All elements after 
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); // Join multiple arrays (which can also be strings, or a mixture of arrays and strings) as 1 Arrays, returning the new array connected 

6. Copy of Array


arrayObj.slice(0); // Returns a copy array of the array, note that 1 New array, not pointing to 
arrayObj.concat(); // Returns a copy array of the array, note that 1 New array, not pointing to 

7. Sorting of Array Elements


arrayObj.reverse(); // Invert the elements (first to last, last to first) and return the array address 
arrayObj.sort(); // Sort array elements and return array addresses 

8. String of array elements


arrayObj.join(separator); // Returns a string that sets each of the 1 Element values are connected to the 1 Start, use in the middle  separator  Separate. 
toLocaleString  , toString  , valueOf : Can be seen as join Special usage of, not commonly used 

2. Three properties of an 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 with 0, the upper and lower bounds of an array are: 0 and length-1, respectively. Unlike most other languages, the length attribute of the JavaScript array is mutable, and this one point requires special attention. When the length attribute is set larger, the state of the whole array does not actually change, only the length attribute becomes larger; When the length attribute is set smaller than the original, the values of elements in the original array whose indexes are greater than or equal to length are all lost. Here is an example that demonstrates changing the length properties:

Code


var arr=[12,23,5,3,25,98,76,54,56,76];
// Defines the 1 Contains 10 An array of numbers 
alert(arr.length); // Display the length of the array 10
arr.length=12; // Increase the length of an array 
alert(arr.length); // Shows that the length of the array has changed to 12
alert(arr[8]); // Display number 9 The value of elements, which is 56
arr.length=5; // Reduce the length of the array to 5 , index equal to or greater than 5 Elements of are discarded 
alert(arr[8]); // Display number 9 Elements have become "undefined"
arr.length=10; // Restores the array length to 10
alert(arr[8]); // Although the length is restored to 10 , but the first 9 Elements cannot be retracted, showing "undefined"

From the above code, we can clearly see the nature of length attribute. However, the length object can not only be set explicitly, but also be modified implicitly. You can use an undeclared variable in JavaScript, as well as an undefined array element (an element with an index greater than or equal to length), where the value of the length attribute is set to the value of the element index in use plus 1. For example, the following code:


var arr=[12,23,5,3,25,98,76,54,56,76];
alert(arr.length);
arr[15]=34;
alert(arr.length);

In the code is also defined first contains 10 numbers of the array, through the alert statement can be seen that its length is 10. Then you use an element with an index of 15, assign it to 15, that is, arr [15] = 34, and then use the alert statement to output the length of the array, resulting in 16. In any case, this is a surprising feature for developers who are used to strongly typed programming. In fact, an array created in the form new Array () has an initial length of 0, and it is the operation on undefined elements that changes the length of the array.

As can be seen from the above introduction, the length attribute is so magical that it can easily increase or decrease the capacity of arrays. Therefore, in-depth understanding of length attributes is helpful to use them flexibly in the development process.

2. prototype attribute

Returns a reference to an object type stereotype. The prototype attribute is common to object.

objectName.prototype

The objectName parameter is the name of the object object.

Description: prototype attribute provides 1 set of basic functions of object class. A new instance of the object "inherits" the operation given to the object prototype.

For array objects, the following example illustrates the purpose of prototype attribute.

Adds a method to an array object that returns the value of the largest element in the array. To accomplish this, declare a function, add it to Array. prototype, and use it.

Code


var testGetArrValue=arrayObj[1]; // Gets the element values of the array 
arrayObj[1]= " This is the new value "; // Assign a new value to an array element 
0

After this code is executed, y holds the maximum value in the array x, or 6.

3. constructor attribute

Represents a function that creates an object.

object.constructor //object是对象或函数的名称。

Explanation: The constructor property is a member of all objects with prototype. They include all JScript native objects except Global and Math objects. The constructor property holds a reference to the function that constructs an instance of a particular object.

For example:


var testGetArrValue=arrayObj[1]; // Gets the element values of the array 
arrayObj[1]= " This is the new value "; // Assign a new value to an array element 
1

Or


var testGetArrValue=arrayObj[1]; // Gets the element values of the array 
arrayObj[1]= " This is the new value "; // Assign a new value to an array element 
2

y = new MyFunc;
if (y.constructor == MyFunc) //  To process (the condition is true). 

For arrays:

y = new Array();


Related articles: