JavaScript array (array) uses a string as the array's underlying method

Source: Internet
Author: User
Tags javascript array

Array is inherited from the Object. It has all the functions and features of the Object. The following describes the situation of objects:
New: var object = new Object ();
Added: object [strIndex] = value; (strIndex is string)
Delete: delete object [strIndex];
Traversal: for (var strObjIndex in object) object [strObjIndex];
As follows:

Copy codeThe Code is as follows:
Var obj = new Object ();
Obj ["first"] = "my ";
Obj ["second"] = "name ";
Obj ["third"] = "is ";
Obj ["fourth"] = "chenssy ";

Because Array inherits objects, Array can also use strings as the underlying Object of the Array:
As follows:

Copy codeThe Code is as follows:
Var array = new Array ();
Array ["first"] = "my ";
Array ["second"] = "name ";
Array ["third"] = "is ";
Array ["fourth"] = "chenssy ";

For the traversal of array numbers, we use the for loop statement. However, this for loop is not in this form:

Copy codeThe Code is as follows:
For (int I = 0; I <arrray. length; I ++)
 

We can use the for/in loop to traverse the array. The for/in loop temporarily assigns the subscript of an array to a variable:

Copy codeThe Code is as follows:
1for (variable in array)

In the first loop, the variable is assigned the lower value of the first element of the array. In the second loop, the variable will be assigned the lower value of the second element of the array, and so on .......
For the array above, use the for/in loop traversal:
Copy codeThe Code is as follows:
For (key in array)

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.