Definition and use of arrays in javascript, javascript Array

Source: Internet
Author: User
Tags javascript array

Definition and use of arrays in javascript, javascript Array

This article describes the definition and usage of arrays in javascript. Share it with you for your reference. The specific analysis is as follows:

Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Script type = "text/javascript">

// [Dictionary array]
Var arr = new Array (); // declare a dynamic Array object arr
Arr [0] = "tom ";
Arr [1] = "jim ";
Arr [2] = "Two Dogs ";
For (var I in arr ){
Alert (I); // output 1, 2, 3 [it does not output vale like C # array, Here output is key: dictionary style]
}

For (var I = 0; I <arr. length; I ++ ){
Alert (arr [I]); // output tom, jim, two dogs
}
*/

// [Dictionary array]
Var dict = new Array (); // declare an Array object dict
Dict ["person"] = "ren"; // dynamically Add a person attribute
Dict ["Port"] = "kou"; // dynamically Add a port attribute
Dict ["hand"] = "shou"; // dynamically Add a hand attribute

For (var item in dict) {// traverses the dict array object: This for loop is equivalent to the foreach traversal in C #, And the syntax is the same, but the foreach becomes the
Alert (item); // output person, mouth, and hand [it does not output vale like C # array, Here output is key: dictionary style]
// Alert (arr [item]) // If You Want To output its value, you can write it like this, and then output: ren, kou, shou.
}

// Since the key obtained through var v in dict has this feature, then we can use this feature to obtain all the members in an object (the object members appear in the form of object keys)
For (var v in document) {// output all the members of the document Object
Document. writeln (v );
}

Alert (dict ["Port"]); // output kou; because the dict array object uses "people", "Port", "hand" as the key, therefore, the value "kou" is obtained based on the "Port" key"

// The array can be declared in a simplified way.
// [Simplified declaration form for common arrays]
Var str = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // This array can be seen as a special case of dict ["person"] = "ren";, that is, key is 0, 1, 2, 3 ....... when the key is 0, the value is 1.
For (var I = 0; I <str. length; I ++ ){
Alert (str [I]); // output 1, 2, 3, 4, 5, 6, 7, 8, 9
}

// [Simplified declaration form for dictionary-style arrays]
Var str = {"tom": 30, "jim": 28, "Two Dogs": 16 };

For (var v in str ){
Alert (v); // output tom, jim, two dogs
}

/*
For (var I = 0; I <str. length; I ++) {// note that keys are not dictionary-style arrays of numbers and cannot be traversed in the form of a for loop. Because str [I], where I is an index and a number
Alert (str [I]);
}*/
</Script>
</Head>
<Body>

</Body>
</Html>

I hope this article will help you design javascript programs.

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.