Using _javascript techniques for JavaScript arrays

Source: Internet
Author: User
Tags array length javascript array

The definition of an array:

Method 1.

Copy Code code as follows:

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

Method 2.

Define and initialize together:

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

Or another way of writing:

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

JavaScript two-dimensional array:

JavaScript uses one-dimensional arrays to simulate two-dimensional arrays:

Method 1.

var arr = new Array ([' A ', ' B ', ' C '],[' d ', ' e ', ' f ']);
ARR[0] Returns the first one-dimensional array, arr[0][0] returns the first element ' a ' of the first one-dimensional array, the same below.

Method 2.

Copy Code code as follows:

Arr=new Array ();
for (i=0;i<100;i++) {
Arr[i]=new Array (...);
}

Method 3.
Copy Code code as follows:

var arr=new Array (
New Array (),
New Array (),
New Array ()
);

Array Length:

The JavaScript array does not need to be set in length, it expands itself, and the array name. Length returns the number of elements

Common functions:

Common functions for arrays

ToString (): Converts an array to a string
toLocaleString (): Converts an array to a string
Join (): Converts an array into a symbolic concatenated string
Shift (): Moves an element of an array's head out
Unshift (): Inserts an element in the header of an array
Pop (): Deletes an element from the tail of an array
Push (): Adds an element to the tail of the array
Concat (): Adding elements to an array
Slice (): Returns the part of an array
Reverse (): Sort the array in reverse order
Sort (): Sorting operations on array
Splice (): Inserts, deletes, or replaces an array element

JavaScript Array Ordering:

Arrayobj.sort (sortfunction)

Parameters

Arrayobj
Array
Sortfunction
Options available. comparison function. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.
The comparison function must return one of the following values:

* Negative value if the first argument passed is smaller than the second argument.
* Zero if two parameters are equal.
* Positive value if the first argument is larger than the second argument

Example:

var testarray=[1,5,2,3,6,4]
Testarray.sort (function (a,b) {return a-b;});
alert (Testarray);

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.