An array is a linearly allocated memory. The element is accessed by calculating the offset.
Unfortunately, JavaScript does not have the same data structures as arrays
As an alternative, JavaScript provides an object of an array of classes, changing the array subscript to a string.
1 array literals (arrays literals)---inherit from Array.prototype, so a number of useful methods are inherited, such as the length property
var empty = [];
var numbers = [
' Zero ', ' one ', ' one ', ' one ', ' three ', ' four ',
' Five ', ' six ', ' seven ', ' eight ', ' nine '
];
EMPTY[1]//undefined
NUMBERS[1]//' one '
Empty.length//0
Numbers.length//10
Object Literal---inherited from object.prototype
var numbers_object = {
' 0 ': ' Zero ',
' 1 ': ' One ',
' 2 ': ' Both ',
' 3 ': ' Three ',
' 4 ': ' Four ',
' 5 ': ' Five ',
' 6 ': ' Six ',
' 7 ': ' Seven ',
' 8 ': ' Eight ',
' 9 ': ' Nine '
};
Note: In most languages, array objects require the same element type, but JavaScript is not.
2 lengths (length)
Unlike most languages, JavaScript has an array length that has no upper bounds and, if stored with an subscript greater than the current length, does not go wrong.
The array will be automatically enlarged.
3 deletion (delete)
Because an array of JavaScript is an object, you can delete an object from the array with the delete operator
Delete Numbers[2];
Numbers is [' zero ', ' one ', Undefined, ' shi ', ' go ']
When this is removed, a "hole" is left in the original position.
So the correct deletion should be using the Splice method The first parameter is the ordinal of the array, the second parameter is the number of deletions
Numbers.splice (2, 1);
Numbers is [' zero ', ' one ', ' Shi ', ' go ']
Note: For large arrays, the efficiency is not high.
4 Enumeration (enumeration)
Note that for-in cannot be guaranteed in order, and may take properties on the prototype chain, so it is not recommended.
General for no problem. The code is as follows
var i;
for (i = 0; i < myarray.length; i + = 1) {
Document.writeln (Myarray[i]);
}
5 easily confusing places (confusion)
A common mistake is to mix arrays and objects.
The rules are simple, using an array when the property name is a small, sequential integer, or the object is used.
You can use the following method to determine whether an array
var Is_array = function (value) {
return Object.prototype.toString.apply (value) = = = ' [Object Array] ';
};
6 Method (Methods)
JavaScript provides a set of methods available for arrays, which are functions stored in Array.prototype
We can augment him, for example, by adding an array to calculate the method
Array.prototype.reduce = function (f, value) {
var i;
for (i = 0; i < this.length; i + = 1) {
Value = f (this[i], value);
}
return value;
};
Examples of using this method.
Create an array of numbers.
var data = [4, 8, 15, 16, 23, 42];
Define and functions. One'll add
Numbers. The other would multiply and numbers.
var add = function (A, b) {
return a + B;
};
var mult = function (A, b) {
return a * b;
};
Invoke The data ' s reduce method, passing in the
Add function.
var sum = data.reduce (add, 0); Sum is 108
Invoke the reduce method again, this time passing
In the multiply function.
var product = Data.reduce (mult, 1);
Product is 7418880
7 Specifying the initial value (Dimensions)
Array.dim = function (dimension, initial) {
var a = [], I;
for (i = 0; i < dimension; i + = 1) {
A[i] = initial;
}
return A;
};
Make an array containing zeros.
var MyArray = Array.dim (10, 0);
JavaScript Good Parts Learning notes-array article