JavaScript Array (array) objects
The role of an array of objects is: Use a separate variable names to store a series of values.
Online examples
Create an array, its assignment:
Examples
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
Bottom of the page you can find more examples.
What is an array?
An array of objects using a single variable to store the name of a series of values.
If you have a set of data (for example: the name of the car), there is a single variable, as follows:
var car2="Volvo";
var car3="BMW";
However, if you want to find out a specific vehicle? And not three, but 300 it? This will not be an easy task!
The best way is to use an array.
Arrays can use a variable to store the name of all values, and can be used to access any variable name value.
Each element in the array has its own ID, in order that it can be easily accessed.
Create an array
Create an array, there are three ways.
The following code defines an array of objects called myCars:
1: a conventional manner:
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";
2: simple manner:
3: literal:
Access Array
By specifying the array name and index number, you can access a specific element.
The following instances can access the first value myCars array:
The following example will change the first element of the array of myCars:
[0] is the first element of the array. [1] is the second element of the array. |
In an array of objects that you can have different
All variables are JavaScript objects. Array elements are objects. Functions are objects.
So, you can have different variable types in the array.
You can include object elements, functions, arrays in an array:
myArray [1] = myFunction;
myArray [2] = myCars;
Array methods and properties
An array of objects using predefined properties and methods:
var y=myCars.indexOf("Volvo") // "Volvo" 值的索引值
A complete array of Object Reference
You can refer to this site on the array complete reference manual for all the properties and methods.
Reference Manual contains a description (and more examples) all the attributes and methods.
A complete array of Object Reference
Create new method
JavaScript is a prototype global constructors. It can build new properties and methods Javascript object.
Example: Create a new approach.
for (i = 0; i <this.length; i ++) {
this [i] = this [i] .toUpperCase ();
}
}
The above example creates a new array of methods for the array lowercase characters to uppercase characters.
More examples
Merge three arrays - concat ()
Elemental composition of a string array - join ()
Remove the last element of the array - pop ()
Added to the end of an array of new elements - push ()
The order of elements in an array reverse the sort - reverse ()
Remove the first element of the array - shift ()
Choose from an array of elements - slice ()
Array sort (in ascending alphabetical order) - sort ()
Numeric sort (in ascending numerical order) - sort ()
Number Sequencing (in descending numerical order) - sort ()
Add an element to the second position in the array - splice ()
Convert a string array to -toString ()
In the beginning of the array to add new elements - unshift ()