The best JavaScript Array (array) objects Tutorial In 2024, In this tutorial you can learn Online examples,Examples,What is an array?,Create an array,Access Array,In an array of objects that you can have different,Array methods and properties,A complete array of Object Reference,Create new method,Example: Create a new approach.,More examples,

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";

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

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

JavaScript Array (array) objects
10/30