Java Script Basics (eight) array arrays object

Source: Internet
Author: User

One, array arrays

Arrays in JavaScript are also one or more collections that have the same data type. Usage is similar to arrays in Java.

Common properties and methods for array objects:

Property:

Length: Gets the size of the array;

Method:

Join (): puts all the elements in the array into a string separated by delimiters.

Sort (): Sorts the array elements.

  1. Creating an array

Grammar:

var a1 = new Array (); Creates an array with a length of 0;
var a2 = new Array (size); Creates an array of the specified length, an array with length of size, and an array of undefined elements
var a3 = new Array (element0, Element1, ..., ELEMENTN); Creates an array based on a predefined value. The length is the number of elements.

  

  2. Assigning values to arrays

(1) First declaration and re-assignment

var pro = new Array (4);

Pro[0] = "Guangdong province";

PRO[1] = "Hubei province";

PRO[5] = "Guangxi province"; The original length is 4, and when you assign a value to an element with subscript 5, the array length becomes 6.

Elements are accessed through the subscript, and elements are traversed through a for loop

(2) Use the character designator to access the element:

pro["GD"] = "Guangdong province";

If you use a string representation you can only use the for: In ... The method is convenient and the For method cannot access the data.

  

(3) Simultaneous initialization of declarations:

var pro = new Array ("Beijing", "Shanghai", "Tianjin", "Chongqing city", "Guangdong province", "Hubei province");

(4) directly initialized to a two-dimensional array:

var citylist = new Array ();

citylist["Guangdong province"] = ["Guangzhou", "Shenzhen", "Dongguan"];

citylist["Hubei province"] = ["Wuhan City", "Xiangyang City"];

  3, the operation of the array:

(1) The array reads:

Array [subscript] or array ["key"] to read

(2) Array traversal:

Traversal through for loop: An array using subscript method

for (Var i=0;i<array.length;i++) {

Alert (Pro[i]);

}

Through for: In.. Traversal: Array using the keyword method

for (var i in array) {

Alert (Pro[i]);

}

A two-dimensional array requires a nested loop to read

for (var i in citylist) {

for (Var j in Citylist[i]) {

Alert (Citylist[i][j]);

}

}

Java Script Basics (eight) array arrays 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.