Home > Web Front-end > JS Tutorial > Introduction to the use of [] and {} objects in javascript_javascript skills

Introduction to the use of [] and {} objects in javascript_javascript skills

WBOY
Release: 2016-05-16 17:40:08
Original
1379 people have browsed it
Copy code The code is as follows:

var a=["apple","peach","banala" ];

The above is another way to define an array, which is equivalent to
Copy code The code is as follows:

var a=new Array();
a.push("apple");
a.push("peach");
a. push("banala");

Copy code The code is as follows:

var b={a:"apple",p:"peach",b:"banala"};

The above is a json object
There are two concise special symbols in front of it [] and {}. They are in the form of an object. [] can not only represent an array, but can also set and access values ​​directly through the properties of the object. For example:
Copy code The code is as follows:

var c=[];
c ["a"]="apple";
c["b"]="banala";

or
Copy Code The code is as follows:

var c={};
c["a"]="apple";
c["b" ]="banala";

Their functions and effects are the same, with only minor differences, which will be discussed later.
You can access it directly through attributes:
Copy code The code is as follows:

alert (c["a"]);

displays "apple".
If you want to traverse, you can pass:
Copy the code The code is as follows:

< ;PRE class=html name="code">for(var key in c)
alert(c[key]);
 
This will display all attribute values.


Of course, there is an each traversal in jquery, and you can also access various properties and values. But this is only the case if


var c={};
, if it is
&lt ;/PRE> <br><PRE class=html name="code"><PRE class=html name="code">var c=[];


No way.


Then use



$.each(c, function(key, val) { <br>alert(key ":" val); <br>});



It is very convenient to use objects. It is much faster than using arrays. The time complexity of finding a certain value in the array is O(n) , and the time complexity of using objects is only O(1), so in most cases objects are used to store values.








 

 
 
 
< /PRE> <br><PRE>

 
 ; 
 
 
 🎜>
 
 
 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template