Arrays are one of the most involved data types since I learned programming. To put it bluntly, arrays are nothing more than key-value pairs. I still remember that when I first joined my job, a PHP foreach loop almost made me vomit blood. Now I think it was really stupid.
An array is a data type that contains or stores encoded values. Each encoded value is called an element of the array ), the encoding of each element is called index ).
Javascript is a magic scripting language, because it is a non-type language, so an array element can have any data type, different elements of the same array can also have different types, this clearly makes C language experts trigger blood three liters.
Create a JavaScript Array
In JavaScript, there are a total of the following methods to create an array:
1. Use the constructor array () of the array object to instantiate the array object with the new operator like creating an object (actually an object.
In this way, an array without elements is created, and the number of elements is null.
// Create an empty array
VaR arr = new array ();
// Its length is -- 0
Alert (ARR. Length );
2. Use the constructor array () to specify the first n elements of an array.
The length of the array created in this way is N, and the subscript of the array is from 0 to n-1.
// Create an array. The first seven digits are 'A', 'B', 'C', 'D', 1, 2, and 3.
VaR arr = new array ('A', 'B', 'C', 'D', 1, 2, 3 );
// The array length is 7.
Alert (ARR. Length );
3. Using the constructor new array (), add an integer directly as the parameter to specify the length of the array. Each element value of the created array is undefined.
// Specify the array length as 10
VaR arr = new array (10 );
Alert (ARR. Length );
// The array element is undefined.
Alert (ARR [0]);
What is the effect if I pass a negative number, zero, letter, or string?
// If no response is returned, array creation fails.
VaR arr = new array (-1 );
Alert (ARR. Length );
// The array length is 0.
VaR arr = new array (0 );
Alert (ARR. Length );
// If no response is returned, array creation fails.
VaR arr = new array ();
Alert (ARR. Length );
// Create an array with a length of 1 and only one string
VaR arr = new array ('A ');
Alert (ARR. Length + "arr [0]'s value is:" + arr [0]);
Er? After reading this, are you surprised? Why are we using the Array () constructor? It's so strange. And whether you want to use the constructor array () to create an array.
The following error occurs:
Array direct quantity
The direct amount of arrays allows you to directly embed the values of an array into a javascript program, just like introducing string text into nicknames to define a direct amount of strings.
To create an array directly, you only need to put a comma-separated value list between square brackets (brackets.
// Use the array directly to give an array and a friend list without full statistics.
// No particular ranking
VaR arr = ['xiaoxiaozi', 'laonb', 'bolo', '1t1g', 'zeric ', 'xiao Ming piao', 'porridge Bu', 'Wilderness '];
// The categories are different, but they are all what I eat this evening.
VaR foot = ['chicken soup stinky beance', 'Spicy tang', 'Ice factory', 'plum ', 'canned peach'];
// Example of a formal point
VaR magic_arr = [['prop sub', {ID: simaopig, age: 25}], [1, true, false], 'all types of data are available, and there are subarrays, haha '];
Read and Write operations on Arrays
Reading array elements is relatively simple. You only need to use square brackets (brackets.
Before talking about this, let's first recall what is used for object reading? That is, the "." operator. What else can be used? Well, that's the square brackets ". This tells us that arrays are actually objects. Well. They are essentially the same
An array has a lower mark, and its value range is an integer greater than or equal to 0 and less than 232-1. If the number is too large, JavaScript cannot be used. Because it will automatically convert it to a "string ".
In JavaScript, array elements are sparse, which means that the subscript of the array will not fall into a continuous number range. Only those elements actually stored in the array can be allocated internally, the rest will not waste your precious memory space.
// Define an array
VaR arr = new array (1, 2, 3, 4, 'A', 'B', 'C ');
// Read the array element. The subscript is in square brackets. Therefore, 2 is returned for this request.
Arr [1]
// Write the array element, 10, overwrite the fourth array element array to (, 'A', 'B', 'C ')
Arr [3] = 10;
For example, we define 0 and 1000 for the array. Only 0 and 1000 subscripts are allocated memory space, and the remaining 999 subscripts are not allocated internal space.
// Declare an empty array
VaR arr = new array ();
Arr [0] = 'xiaoxiaozi ';
Arr [1000] = 'simaopig ';
// 1001mm. From 0 to 1000
Alert (ARR. Length );
// No definition, undefined
Alert (ARR [999]);
Array Length
Use the "instance attribute" of "length" to obtain the array length in Javascript. If you are not clear about the concept of "instance attribute", I suggest you take a look at my previous log. Haha.
The subscript of the array starts from 0. Therefore, the maximum subscript of the array of n elements is n-1 and the length is N.
As we have mentioned above, the subscript of the array must be smaller than 232-1, which means that the maximum length of the Javascript array length is 232-1.
You can assign an object to an array element, but the object does not have the Length attribute. Therefore, the Length attribute is the most important feature of the array. Haha.