Processing when the object key value is a number

Source: Internet
Author: User

First, the solution:

Obtained by A[key] .

The specific scenario is this:

There is a key of an object is a number, this time using the form of a.b can not get the corresponding value,

Such as:

A. ' 111 ' will cause an error.

Special Note:

a[' 111 '] and a[111] Both are the same, no difference, this usage is the same as the array with subscript access.

another :

When the object's key is a variable and is used in a loop, we can dynamically generate the object by data[' key ', eg:

Summarize:

1. The object's key should generally use a valid variable name (a letter or underscore that begins with a string containing letters, underscores, and numbers), in which case the value can be obtained by a.key or a[' key ').

2. When the special scene key is a number, the corresponding value value can only be obtained by a[' key '.

If you create a map object that corresponds to ID one by one in the database,

You can do the key directly with the ID number, although you can add a letter prefix to it to conform to the standard of the legal variable name,

And let its data be accessed through the Data.key way,

But if the amount of data is very large,

Add a prefix to each ID, and the + character join operation also consumes performance.

Especially in cases where it is seldom necessary to use the Data.key way to access attributes,

Then you can discard this call method, directly with the number of the key is also possible,

There seems to be no other loss except the key name does not conform to the standard of the legal variable name;

Processing when the object key value is a number

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.