var a=["apple","peach","banala" ];
The above is another way to define an array, which is equivalent to
var a=new Array();
a.push("apple");
a.push("peach");
a. push("banala");
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:
var c=[];
c ["a"]="apple";
c["b"]="banala";
or
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:
alert (c["a"]);
displays "apple".
If you want to traverse, you can pass:
< ;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
< ;/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>
;
🎜>