Introduction to using [] and {} objects in javascript

Source: Internet
Author: User

Copy codeThe Code is as follows:
Var a = ["apple", "peach", "banala"];

The above is another definition of an array, which is equivalent
Copy codeThe Code is as follows:
Var a = new Array ();
A. push ("apple ");
A. push ("peach ");
A. push ("banala ");

Copy codeThe Code is as follows:
Var B = {a: "apple", p: "peach", B: "banala "};

The preceding is a json object.
There are two concise special symbols [] and {}. they are in the form of objects. [] can not only represent arrays, but can directly set values and access values through object attributes. For example:
Copy codeThe Code is as follows:
Var c = [];
C ["a"] = "apple ";
C ["B"] = "banala ";

Or
Copy codeThe Code is as follows:
Var c = {};
C ["a"] = "apple ";
C ["B"] = "banala ";

Their functions and functions are the same, with only minor differences.
During access, you can directly use the following attributes:
Copy codeThe Code is as follows:
Alert (c ["a"]);

"Apple" is displayed ".
To traverse, you can use:
Copy codeThe Code is as follows:
<PRE class = html name = "code"> for (var key in c)
Alert (c [key]); </PRE>
<PRE> </PRE>
In this way, all attribute values are displayed.
<P> </P>
<P> of course, jquery has an each traversal function. You can also access each attribute and value. But this is only </P>
<PRE class = javascript name = "code"> var c ={}; </PRE>.
<PRE> </PRE>
<PRE class = html name = "code"> <PRE class = html name = "code"> var c = []; </PRE> <BR>
No. <BR>
<P> </P>
<P> then use </P>
<P> </P>
<PRE class = javascript name = "code"> $. each (c, function (key, val ){
Alert (key + ":" + val );
}); </PRE> <BR>
<P> </P>
<P> it is convenient to use an object. It is much faster than to use an array. the time complexity of searching for a value in an array is O (n ), the time complexity of using objects is only O (1), so objects are used to store values in most cases. </P>
<P> <BR>
</P>
<P> <BR>
</P>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
</PRE>

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.