Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript Array (array) objects

The role of an array of objects is: Use a separate variable names to store a series of values.


Examples

Online examples

Create an array, its assignment:

Examples

var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

try it"

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 car1="Saab";
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:

var myCars=new Array();
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";

2: simple manner:

var myCars=new Array("Saab","Volvo","BMW");

3: literal:

var myCars=["Saab","Volvo","BMW"];


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:

var name=myCars[0];

The following example will change the first element of the array of myCars:

myCars[0]="Opel";

lamp [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 [0] = Date.now;
myArray [1] = myFunction;
myArray [2] = myCars;


Array methods and properties

An array of objects using predefined properties and methods:

var x=myCars.length // myCars 中元素的数量
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.

Array.prototype.myUcase = function () {
for (i = 0; i <this.length; i ++) {
this [i] = this [i] .toUpperCase ();
}
}

try it"

The above example creates a new array of methods for the array lowercase characters to uppercase characters.


Examples

More examples

Merge two arrays - concat ()

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 ()