How to Use Array objects in JavaScript _ javascript skills

Source: Internet
Author: User
The Array object in JavaScript is a dynamic Array, a Stack, or a Dictionary. It is a dynamic Array because data is dynamically added;

The Code is as follows:


Var myarr = new Array ();
Myarr [0] = 1;
Myarr [1] = 2;
Myarr [2] = 3;
Myarr [3] = 23;
Myarr [4] = 11;


Use for traversal;
It is a dictionary object because it can be accessed as a key value:

The Code is as follows:


Var dictionary = new Array ();
Dictionary ["Xie longbao"] = "xielongbao ";
Dictionary ["Zhou baucui"] = "zhoubaocui ";
Dictionary ["Xie Xiaoyue"] = "xiexiaoyue ";
Alert (dictionary ["Xie longbao"]);
Alert (dictionary. Xie longbao );
For (var key in dictionary ){
Alert ("key:" + key + "value:" + dictionary [key]);
}


Use for-in for traversal. The array is a special case of dic. The array key is an integer and the dic key is a string. Therefore, the array can also be traversed using for-in; in addition, because the object members in js also exist in the form of keys, we can use for-in to view the js object members;

Simplified Syntax of arrays in js:

Var arr = [1, 2, 3, 4];
Simplified Writing of dic in js:
Var arrdic = {"jim": 30, "tom": 20 };
This is often used for interaction with the server, because the data transmitted from the server is in json format, that is, the key-value pairs in Javascript facilitate front-end operations;
It is a Stack because it has pop (), push (), and other methods for operating the stack;
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.