JavaScript Array (array) method of using string as an array subscript _ basics

Source: Internet
Author: User
Tags javascript array

Array is inherited from object. It has all the functions and properties of object. Here is the case of object:
NEW: var object = new Object ();
Add: object[strindex] = value; (Strindex to String)
Delete: delete Object[strindex];
Traversal: for (var strobjindex in object) Object[strobjindex];
As follows:

Copy Code code as follows:

var obj = new Object ();
Obj["a" = "my";
Obj["Second"] = "name";
obj["Third"] = "is";
obj["Fourth"] = "Chenssy";

Because array inherits object, array can also use a string as the subject of the array:
As follows

Copy Code code as follows:

var array = new Array ();
Array["a" = "my";
Array["Second"] = "name";
array["Third"] = "is";
array["Fourth"] = "Chenssy";

For the traversal of array numbers, we take a For loop statement. But this for loop is not in this form:

Copy Code code as follows:

for (int i = 0;i<arrray.length;i++)

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

Copy Code code as follows:

1for (variable in array)

At the first loop, the variable variable is assigned the subscript value of the first element of the array, and, on the second loop, the variable variable will be assigned the subscript value of the second element of the array of arrays;
For the array above, use the for/in loop to iterate through:

Copy Code code as follows:

For (key in array)

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.