Basic knowledge of array structure in JavaScript programming

Source: Internet
Author: User
Tags arrays

The function of an array object is to store a series of values using a separate variable name.
Create an array and assign it a value:
Instance

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

What is an array?
an Array object uses a separate variable name to store a series of values.
If you have a set of data (for example: car name), there are individual variables as follows:

var car1= "Saab";
var car2= "Volvo";
var car3= "BMW";

However, if you want to find out about a particular car? And not 3 cars, but 300 cars? This will not be an easy thing!
The best way is to use arrays.
An array can store all values with a variable name, and you can access any value with the variable name.
Each element in the array has its own ID so that it can be accessed easily.
Create an array
There are three methods of creating an array.
The following code defines an array object named Mycars:
1: Conventional way:

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

2: Concise Way:

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

3: literal:

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

accessing arrays
you can access a particular element by specifying the array name and the index number.
The following instance can access the first value of the Mycars array:

var name=mycars[0];

The following instance modifies the first element of the array Mycars:

Mycars[0]= "Opel";

Lamp [0] is the first element of the array. [1] is the second element of the array.

You can have different objects in an array.
all JavaScript variables are objects. An array element is an object. function is an object.
So you can have different types of variables in the array.
You can include object elements, functions, and arrays in an array:

Myarray[0]=date.now;
myarray[1]=myfunction;
Myarray[2]=mycars;

Array methods and properties
use array objects to predefined properties and methods:

var x=mycars.length       //The number of elements in Mycars
var y=mycars.indexof ("Volvo")  //The index Positio N of "Volvo"


Create a new method
The prototype is a JavaScript global constructor. It can build properties and methods for new JavaScript objects.
Instance: Creates a new method.

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

The example above creates a new array method for converting an array lowercase character to uppercase.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.