Use of javascript Arrays

Source: Internet
Author: User
Tags javascript array

Array definition:

Method 1.

Copy codeThe Code is as follows:
Var mycars = new Array ()
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"

Method 2.

Definition and initialization together:

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

Or another method:

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

Javascript two-dimensional array:

Javascript uses a one-dimensional array to simulate a two-dimensional array:

Method 1.

Var arr = new Array (['A', 'B', 'C'], ['D', 'E', 'F']);
Arr [0] returns the first one-dimensional array, and arr [0] [0] returns the first element 'a' of the first one-dimensional array, the same below.

Method 2.
Copy codeThe Code is as follows:
Arr = new Array ();
For (I = 0; I <100; I ++ ){
Arr [I] = new Array (...);
}

Method 3.
Copy codeThe Code is as follows:
Var arr = new Array (
New Array (),
New Array (),
New Array ()
);

Array length:

You do not need to set the length of the javascript array. The array name. length returns the number of elements.

Common functions:

Array common functions

ToString (): converts an array into a string.
ToLocaleString (): converts an array into a string.
Join (): converts an array into a string connected with symbols.
Shift (): removes an element from the array header.
Unshift (): insert an element in the header of the array.
Pop (): deletes an element from the end of the array.
Push (): Add an element to the end of the array.
Concat (): add elements to the array
Slice (): returns the part of the array
Reverse (): sorts arrays in reverse order.
Sort (): sorts arrays.
Splice (): insert, delete, or replace an array element.

Javascript array sorting:

Arrayobj. sort (sortfunction)

Parameters

ArrayObj
Array
SortFunction
Optional. Comparison functions. If this parameter is omitted, the elements are listed in ascending order of ASCII characters.
The comparison function must return one of the following values:

* Negative value. If the first parameter is smaller than the second parameter.
* Zero. If the two parameters are equal.
* Positive value. If the first parameter is greater than the second parameter

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.