JavaScript -- array uses string as subscript

Source: Internet
Author: User

Reading todayJavascriptWhen, suddenly find the ArrayArrayThe character string can be used as its subscript. I still don't know before. In my opinion, how can I use a character string for Array traversal? Ah .. Ignorant.....

FirstArrayYesfromObjectIt inherits. It hasObjectAll functions and features. Below isObject:

Create: var object = new object (); add: object [strindex] = value; (strindex is string) Delete: delete object [strindex]; traverse: for (VAR strobjindex in object) object [strobjindex];

As follows:

 

 
1VaROBJ =NewObject ();2OBJ ["first"] = "my";3OBJ ["second"] = "name";4OBJ ["third"] = "is";5OBJ ["fourth"] = "chenssy ";

 

BecauseArrayInheritanceObject, ThenArrayYou can also use a string as the underlying object of the array:

As follows:

 

1VaRArray =NewArray ();2Array ["first"] = "my";3Array ["second"] = "name";4Array ["third"] = "is";5Array ["fourth"] = "chenssy ";

 

ForArrayNumber traversal, we useForLoop statement. But thisForLoop is not in this form:

 
1 For(IntI = 0; I <arrray. length; I ++)

We can useFor/inLoop to traverse the array.For/inIn a loop, the subscript of an array is temporarily assigned to a variable:

 
1 For(VariableInArray)

In the first cycle, the variable variable is assigned as an array array indicates the lower value of the first element. In the second loop, variable variable will be assigned an array array the lower value of the second element, and so on .......

ForArrayArray, usingFor/inLoop traversal:

 
1 For(KeyInArray)

 

 

 

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.