Arrays are an important data storage type in javascript. Next I will summarize how to create, define, delete, and modify an array length instance in js.
Define an array
The Code is as follows: |
Copy code |
Var a = new Array ("a", "B", "c "); Method 1. Var mycars = new Array () Mycars [0] = "Saab" Mycars [1] = "Volvo" Mycars [2] = "BMW" |
Method 2.
Definition and initialization together:
The Code is as follows: |
Copy code |
Var mycars = new Array ("Saab", "Volvo", "BMW ") |
Or another method:
The Code is as follows: |
Copy code |
Var mycars = ["Saab", "Volvo", "BMW"]; |
Loop read
The Code is as follows: |
Copy code |
For (I = 0; I <a. length; I ++) {// read cyclically Document. write (I + ":" + a [I] + "<br/> "); } Document. write ("toString ():" + a. toString () + "<br/>"); // toString (), represented by a string Document. write ("join ():" +. join ("") + "<br/>"); // join (), which uses the string parameter after join as the delimiter to generate a string. If the parameter is a half-width comma, the effect is the same as that of toString (). Document. write ("reverse ():" +. reverse (). toString () + "<br/>"); // reverse (). generates a new array in reverse order of elements in the array. Document. write ("valueOf ():" + a. valueOf () + "<br/>"); // valueOf (), the value of the output Array |
// Two-dimensional array
The Code is as follows: |
Copy code |
Var B = new Array (3); // defines an Array with a length of 3. B [0] = new Array ("a", 1 ); B [1] = new Array ("B", 2 ); B [2] = new Array ("c", 3 ); For (I = 0; I <B. length; I ++) {// cyclically outputs each element of each dimension For (j = 0; j <B [I]. length; j ++ ){ Document. write ("B [" + I + "] [" + j + "]:" + B [I] [j] + "<br/> "); } } |
Array length:
You do not need to set the length of the javascript array. The array name. length returns the number of elements.
The following is an example of changing the length attribute:
The Code is as follows: |
Copy code |
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:
The Code is as follows: |
Copy code |
Var arr = [,]; Alert (arr. length ); Arr [15] = 34; Alert (arr. length ); |
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.
Additional message:
SupNate: no specific example. I think that I understand the length attribute and understand the array in javascript. Array is the basis for many examples. Because it is quite different from other languages, let's talk about it separately. For example, the pop Method for arrays not supported by browsers of earlier versions is as follows:
The Code is as follows: |
Copy code |
Array. prototype. pop = function (){ If (0 = this. length) return; Var temp = this [this. length-1]; This. length --; Return temp; } |
In this way, the array can be used as a stack.