Introduction to [] and {} object usage in JavaScript _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

var a=["Apple", "Peach", "Banala"];

This is another way of defining an array, which is equivalent to
Copy Code code as follows:

var a=new Array ();
A.push ("Apple");
A.push ("Peach");
A.push ("Banala");

Copy Code code as follows:

var b={a: "Apple", p: "Peach", B: "Banala"};

This is a JSON object
There are two simple special symbols [] and {}. They are the form of an object where [] not only can you represent an array, you can set values and access values directly from the object's properties. For example:
Copy Code code as follows:

var c=[];
C["A"]= "Apple";
c["B"]= "Banala";

Or
Copy Code code as follows:

var c={};
C["A"]= "Apple";
c["B"]= "Banala";

Their function and function are the same, only a small difference, said later.
Access can be accessed directly by the attribute:
Copy Code code as follows:

Alert (c["a"]);

Displays "Apple".
To traverse, you can pass:
Copy Code code as follows:

<pre class=html name= "code" >for (Var key in C)
Alert (C[key]);</pre>
<PRE></PRE>
This will show all the property values.
<P></P>
<P> of course, there is a each traversal in jquery, and you can access individual properties and values. But this is only when </P>
<pre class=javascript name= "code" >var c={};</pre> situation, if it is
<PRE></PRE>
<pre class=html name= "code" ><pre class=html name= "code" >var
You can't. <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> using objects is convenient, more efficient than using arrays, the time complexity of finding a value in an array is O (n), and the time complexity of using an object is only O (1), so in most cases the object is used to store the value. </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.