HTML Dom tutorial 45-javascript array object

Source: Internet
Author: User

HTML Dom tutorial 45-javascript array object

 

1: array object Method
Method Description FF N IE
Concat () Concatenate two or more arrays and return results. 1 4 4
Join () Put all elements of the array into a string. The elements are separated by the specified delimiter. 1 3 4
Pop () Delete and return the last element of the array 1 4 5.5
Push () Add one or more elements to the end of the array and return a new length. 1 4 5.5
Reverse () The order of elements in the array is reversed. 1 3 4
Shift () Delete and return the first element of the array 1 4 5.5
Slice () Returns the selected element from an existing array. 1 4 4
Sort () Sorts elements of an array. 1 3 4
Splice () Delete elements and add new elements to the array. 1 4 5.5
Tosource () Representing the objectSource code 1 4 -
Tostring () Converts an array to a string and returns the result. 1 3 4
Tolocalestring () Converts an array to a local array and returns the result. 1 3 4
Unshift () Add one or more elements to the beginning of the array and return a new length. 1 4 6
Valueof () Returns the original value of the array object. 1 2 4
2: attributes of the array object
Attribute Description FF N IE
Constructor A reference to the function that creates this object 1 2 4
Index   1 3 4
Input   1 3 4
Length Sets or returns the number of elements in the array. 1 2 4
Prototype Allows you to add attributes and methods to objects 1 2 4

 

3: Join () method of Array

Arrayobject. Join (Separator(Optional). The separator parameter specifies the delimiter to use. If this parameter is omitted, a comma is used as the separator.

In this example, we will create an array and put all its elements into a string:

<SCRIPT type = "text/JavaScript">

VaR arr = new array (3)
Arr [0] = "George"
Arr [1] = "John"
Arr [2] = "Thomas"

Document. Write (Arr. Join ())

</SCRIPT>

Output:

George, John, Thomas

4: shift () method

Note: This method will change the length of the array.

Tip: To delete and return the last element of the array, use the POP () method.

Instance

In this example, an array is created and the first element of the array is deleted. Note that this will also change the length of the array:

 
<SCRIPT type = "text/JavaScript">

VaR arr = new array (3)
Arr [0] = "George"
Arr [1] = "John"
Arr [2] = "Thomas"

Document. Write (ARR + "<br/> ")
Document. Write (ARR. Shift () + "<br/> ")
Document. Write (ARR)

</SCRIPT>

Output:

George, John, Thomas
George
John, Thomas

 5: slice () method

Syntax: arrayobject. Slice (START, end ).

Parameters Description
Start Required. Specifies where to start selection. If it is a negative number, it specifies the position starting from the end of the array. That is to say,-1 refers to the last element,-2 refers to the second to last element, and so on.
End Optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the elements starting from the end of the array.

Note: You can use negative values to select elements from the end of the array.

Note: If the end is not specified, the slice () method selects all elements from start to the end of the array.

In this example, we will create a new array and display the elements selected from it:

<SCRIPT type = "text/JavaScript">

VaR arr = new array (3)
Arr [0] = "George"
Arr [1] = "John"
Arr [2] = "Thomas"

Document. Write (ARR + "<br/> ")
Document. Write (ARR. Slice (1) + "<br/> ")
Document. Write (ARR)

</SCRIPT>

Output:

 
George, John, Thomas
John, Thomas
George, John, Thomas

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.