An array is an orderly combination of values. Each value is called an element, and each element has a position in the array, represented by a number, called an index.
JavaScript arrays are untyped: The elements of an array can be of any type, and different elements in the same array may have different types. The elements of an array may be objects or other arrays, which allow you to create more complex data structures, such as arrays of objects and arrays.
The index of the JavaScript array is the 32-bit value of the basic zero: The index of the first element is 0, the maximum index is 4 294 967 294 (2 32), and the array can hold up to 2 4 294 967 elements.
JavaScript arrays are dynamic: they grow or shrink as needed, and when an array is created without having to declare a fixed size or change the size of an array without having to reassign space, the JavaScript array may be sparse: The index of an array element is not necessarily contiguous, This property is the number of array elements. To sparse arrays, length is larger than the index of all elements.
A JavaScript array is a special form of a JavaScript object, and an array index is actually similar to a property name that happens to be an integer. We will discuss more specialized arrays in this article (chapter). Usually the implementation of an array is optimized, and using an array index to access an array element is generally much faster than accessing regular object properties.
The array inherits from the Array.prototype property, which defines a rich set of array manipulation methods, and sections 8 and 9 cover this. Most of these methods are generic, which means that they are not only valid for a true array, but also for "class array objects". Section 11 discusses class array objects. In ECMAScript5, a string behaves like a character array, and section 12 is discussed.
1. Creating an array
Creating an array with direct amounts is the simplest way to separate groups of elements in square brackets with commas, such as:
var empty = []; var primes = [2, 3, 5, 7, one]; var true // three different types of elements and the end of a comma
The values in the array's direct quantities are not necessarily constants, they can be arbitrary expressions
var base = 1024x768; var table = [Base, base + 1, base + 2, base + 3]
It can contain direct amounts of objects or other array direct quantities
var b = [[1,{x:1,y:2}],[2,{x:3,y:4}]];
If you omit a value from the array's direct amount, the omitted value element is given the undefined
var // There are three elements in the array, and the second value is undefined var // two elements, all undefined.
Calling the constructor array () is a way to create an array. Call constructors in three different ways
- No arguments at call
- Called when there is a value that is a parameter, which specifies the length
- Explicitly specify two or more arrays of elements or a non-array element of an array
// no arguments at call var a =new Array ();
The method creates an empty array without any elements, equal to the direct amount of the array [].
var New Array (10);
This method creates an array of the specified length, which can be used to allocate an array space when the number of required elements is known beforehand. Note that there are no stored values in the array. Even the index of the array, "0", "1", etc. is not yet defined.
var New Array (5, 4, 3, 2, 1, "testing,testing")
In this form, the constructed function parameter will become the new array element, and it is much simpler to use the array literal than it is to use the array () constructor.
2. Reading and writing of array elements
Use the [] operator to access an element in an array. The reference to the array is on the left side of the square brackets. In square brackets is an arbitrary expression that returns a non-negative integer value. You can use this expression to read or write an element in an array. As a result, the following code is a legitimate JavaScript statement
var a = ["World"]; // var value = a[0]; // read No. 0 element a[1] = 3.14; // write a 1th element i = 2 = 3; // write a 2nd element A[i + 1] = "Hello"; // write 3rd element A[a[i]] = a[0] // Read the No. 0 and 2nd elements, and write the 3rd element
keep in mind that arrays are special forms of objects. Using square brackets to access an array element is like using square brackets to access an object's properties . JavaScript replaces the established array index with a string--index 1 becomes "1" and then uses it as the property name. There's nothing special about converting an indexed value from a number to a string: You can do this for regular objects as well:
// create a normal object O[1] = "none"; // index it with an integer
The special thing about arrays is that when you use a non-negative integer that is less than 2 for a 32-second square as the property name, the array automatically maintains its length property value. As above, create an array of only one element, and then assign values at 1, 2, 3, and when we do so, the length of the array becomes 4
It is useful to have an index of a clear region score group and an array of attribute names. All indexes are their property names, but only 0 to (2 of 32 square-2) integer property names are indexes. All arrays are objects, and you can create properties of any name for them. However, if the property used is an index of an array, the special behavior of the array is to update their length property values as needed.
Note that you can use a negative or non-integer to index an array. In this case, the numeric value is converted to a string. The string is used as the property name. Since the name is not a non-negative integer, it can only be used as a regular object property, not as an array. Similarly, if you happen to use a string that is a non-negative integer, it is treated as an array index, not as an object's property. The same thing happens when you use a floating-point number equal to an integer:
true // This will create a property named "-1.23" // This is the 1001th element of the array // and a[1] are equal
In fact, an array index is just a special type of object property name, which means that the JavaScript array does not have the concept of "out of Bounds" errors. When you try to query a property that does not exist in the object, no error is made. Only the undefined value is obtained. Similar to objects, this is true for objects as well.
Since arrays are objects, they can inherit elements from the prototype. In ECMAScript5, arrays can define getter and setter methods for elements (6.6). If an array does inherit elements or getter and setter methods that use elements, you should expect it to use a non-optimized code path: The time to access the elements of this array is similar to the time it takes to find regular object properties.
3. Sparse arrays
A sparse array is an array that contains a discontinuous index starting at 0. Typically, the value of the length property of an array represents the number of elements in the array. If the array is sparse, the value of length is much larger than the number of elements. You can create a sparse array by using the array constructor or simply specifying that the index value of the array is greater than the current array length.
New Array (5); // There are no elements in the array, but the A.length value is 5 // creates an empty array with a value of length of 0 A[1000] = 0; // an assignment adds an element, but sets the length value to 1001
You can also create a sparse array after the delete operator.
Sparse arrays are typically slower to implement than dense arrays, and memory utilization is higher, and the time to find elements in such arrays is as long as regular object properties.
Note that sparse arrays are not created when values are omitted from the array's direct amount. The omitted element is present in the array and its value is undefined. There are some subtle differences between this and the array elements at all. You can use the in operator to detect the difference between the two.
var // array is [undefined,undefined,undefined] var New // the array has no elements at all inch // =>true A1 has an element at index 0; inch // =>false A2 no element at index 0
When using the for/in cycle, the difference between A1 and A2 is also obvious (section 6th)
It is important to note that when omitting the value in the array's direct amount (using a continuous comma, such as [1,,,, 3]), the resulting sparse array, the omitted Shiba is nonexistent:
var // the array has no elements at this time, with a length of 1 var // at this point the array contains an element with a value of undefined inch // =>false:a1 no element at index 0 inch // =>TRUE:A2 has a value of undefined element at 0
In some older implementations, such as (FIREFOX3), where there is a continuous comma, the operation to insert the undefined value differs from this, in which [1,,3] and [1,undefined,3] are identical.
Understanding sparse arrays is part of understanding the true nature of javascript arrays. However, the vast majority of arrays actually encountered are not sparse arrays. And, if you encounter sparse arrays, your code is likely to treat them like non-sparse arrays, except that they contain some undefined values.
4. Length of the array
Each array has a length property. This is the attribute that distinguishes it from regular JavaScript. To a dense array, the value of the Length property represents the number of elements in the element, which is 1 larger than the maximum number of indexes in the array:
// =>0: Array has no elements // =>3 Max is index of 2,length to 3
When the array is sparse, the value of length is much greater than the number of elements. And all we can say about this is that the array length is guaranteed to be greater than the index value of each of its elements. In other words, in an array, the index value of an element must not be found to be greater than its length. to maintain the same rule, the array has two special behaviors. The first one is as described above: If you assign a value to an array element, its index i is greater than or equal to the length of the existing array, the value of length is set to i+1.
The second special behavior is to set the length property to a nonnegative integer n less than the current length, and the elements in the current array whose index values are greater than or equal to n are removed from it, for example:
var // Starting from an array of 5 elements // now A is [1, 2, 3] // Delete all elements A is [] // length is 5, but no element, just line new Array (5)
You can also set the length property value of an array to be greater than the current length. A new element is not actually added to the array, it simply creates an empty area at the end of it.
In ECMAScript5, you can use Object.defineproperty () to turn the properties of an array into read-only:
var a = [1, 2, 3]; false }); = 0; Console.log (a); // = = [1, 2, 3]
Similarly, if you have an array element that cannot be configured, you cannot delete it. If it cannot be deleted, the length property cannot be set below the index value of the non-configurable element (see Section 6.7, Object.seal () and the Object.freeze () method).
5. Addition and deletion of array elements
We've seen the simplest way to add an array element: Assign a value to a new index
// start is an empty array // want to add elements to them A[1] = "one"; A; // = = ["Zero", "one"]
You can use the push () method to add one or more elements at the end of an array ;
Pressing an element at the end of the array is the same as assigning a value to the array a[a.length, you can use the Unshift () method to insert an element into the header and move the other elements to a higher index;
// start is an empty array // adds an element at the end. A = ["zero"] A.push ("One", "one"); Add two additional elements A; =>["Zero", "one", "One", "one", " a.unshift" ("Start"); A; // = = ["Start", "zero", "one", "both"]
You can delete an array element by using the delete operator as you would delete an object property:
A = [1, 2, 3]; Delete a[1]; inch // =>false: Array index 1 is not defined in the array // =>3:delete operation does not affect the length of the array
Deleting an array element is similar to assigning a value to it undefined (but with some subtle differences). Using delete on an array element does not modify the length property of the array, nor does it move the element from the high index to fill the element blank that has been deleted. If you delete an element from an array, it becomes a sparse array .
As you can see from the above, simply setting the length property also removes elements from the tail of the array. The array is used by the pop () method (which uses it with push), which reduces the length by 1 and returns the value of the deleted element. There is also a shift () method (which is used with unshift ()) to remove an element from the head of the array. Unlike delete, the shift () method moves all the elements down to 1 lower than the current element index (the contents of pop () and shift () are covered in section 8 of this article).
Finally,splice () is a common way to insert, delete, or replace an array element method. It modifies the length property as needed and moves the roundness to a higher or lower index (detailed in section 8 of this article).
....
(This article is not finished, the following sections will be updated later, please note that you can browse the first 6 chapters)
6. Array traversal
7. Multidimensional arrays
8. Array methods
Array methods in 9.ECMASCRIPT5
10. Array type
11. Class Array Objects
12. String as an array
Seventh chapter: JavaScript arrays