JavaScript Array (Array) object

Source: Internet
Author: User
Tags javascript array

One, define the array

An array object is used to store a series of values in a separate variable name.

To create the syntax for an Array object:
New Array (); New Array (size); New Array (element0, Element1, ..., ELEMENTN);
Parameters

The parameter size is the expected number of array elements. Returns an array of length fields that will be set to the value of size .

Parameter element ..., elementn is a list of parameters. When you use these parameters to call the constructor array (), the elements of the newly created array are initialized to these values. Its length field is also set to the number of parameters.

return value

Returns the newly created and initialized array.

If you do not use parameters when calling the constructor array (), the returned array is empty and the length field is 0.

When the constructor is called, only one numeric argument is passed to it, and the constructor returns an array with the specified number and element undefined.

When an array () is called by another parameter, the constructor initializes the array with the value specified by the parameter.

When a constructor is called as a function, and the new operator is not used, its behavior is exactly the same as when it is called with the new operator.

We use the keyword new to create an array object. The following code defines an array object named MyArray:

var myarray=New Array ()

There are two ways to assign values to an array (you can add as many values as you want to define as many variables as you need).

1:
var mycars=New  Array () mycars[0]="Saab"mycars[  1]="Volvo"mycars[2]="BMW "

You can also use an integer argument to control the capacity of an array:

var mycars=New Array (3) mycars[0]="Saab" mycars[1]="Volvo"mycars[2]="  BMW"
2:
var mycars=New Array ("Saab","Volvo",  "BMW")

Note: If you need to specify numeric or logical values within an array, then the variable type should be a numeric variable or a Boolean variable, not a character variable.

Two, accessing the array

By specifying the array name and the index number, you can access a particular element.

Here is the line of code:

document.write (mycars[0])

Here is the output:

Saab

Third, modify the values in the existing array

If you want to modify the values in an existing array, simply add a new value to the specified label:

mycars[0]="Opel";

Now, the above code:

document.write (mycars[0]);

Will output:

Opel

Four, example

Create an array, assign a value to it, and then output the values.

"Text/javascript">varMycars =NewArray () mycars[0] ="Saab"mycars[1] ="Volvo"mycars[2] ="BMW" for(i=0; i<mycars.length;i++) {document.write (Mycars[i]+"<br/>")}</script></body>

The effect is as follows:

Use the for...in declaration to loop through the elements in the output array.

"Text/javascript">varxvarMycars =NewArray () mycars[0] ="Saab"mycars[1] ="Volvo"mycars[2] ="BMW" for(xinchmycars) {document.write (mycars[x]+"<br/>")}</script></body>

The effect is as follows:

Use the Concat () method to merge two arrays.

"Text/javascript">vararr =NewArray (3) arr[0] ="George"arr[1] ="John"arr[2] ="Thomas"varARR2 =NewArray (3) arr2[0] ="James"arr2[1] ="Adrew"arr2[2] ="Martin"document.write (Arr.concat (arr2))</script></body>

The effect is as follows:

Use the Join () method to compose all elements of an array into a string.

"Text/javascript">vararr =NewArray (3); arr[0] ="George"arr[1] ="John"arr[2] ="Thomas"document.write (Arr.join ());d Ocument.write ("<br/>");d Ocument.write (Arr.join ("."));</script></body>

The effect is as follows:

Use the sort () method to literally sort the array.

"Text/javascript">vararr =NewArray (6) arr[0] ="George"arr[1] ="John"arr[2] ="Thomas"arr[3] ="James"arr[4] ="Adrew"arr[5] ="Martin"document.write (arr+"<br/>") document.write (Arr.sort ())</script></body>

The effect is as follows:

Use the sort () method to sort the arrays numerically.

"Text/javascript">function Sortnumber (A, b) {returnAB}vararr =NewArray (6) arr[0] ="Ten"arr[1] ="5"arr[2] =" +"arr[3] =" -"arr[4] =" +"arr[5] ="1"document.write (arr+"<br/>") document.write (Arr.sort (sortnumber) )</script></body>

The effect is as follows:

Five, Array object properties
Properties Description
Constructor Returns a reference to the array function that created this object.
Length Sets or returns the number of elements in the array.
Prototype gives you the ability to add properties and methods to an object.
Six, Array object method
Method Description
Concat () Joins two or more arrays and returns the result.
Join () Put all the elements of the array into a string. element is delimited by the specified delimiter.
Pop () Delete and return the last element of the array
Push () Adds one or more elements to the end of the array and returns the new length.
Reverse () Reverses the order of the elements in the array.
Shift () Delete and return the first element of the array
Slice () Returns the selected element from an existing array
Sort () Sorting elements of an array
Splice () Deletes the element and adds a new element to the array.
Tosource () Returns the source code for the object.
ToString () Converts the array to a string and returns the result.
toLocaleString () Converts the array to a local array and returns the result.
Unshift () Adds one or more elements to the beginning of the array and returns the new length.
ValueOf () Returns the original value of an array object
Refer to the JavaScript Array object reference manual for details

JavaScript Array (Array) object

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.