[] Can not only represent arrays, but can directly set values and access values through object attributes. Next we will introduce the use of [] and {} objects, if you are interested, refer
The Code is as follows:
Var a = ["apple", "peach", "banala"];
The above is another definition of an array, which is equivalent
The Code is as follows:
Var a = new Array ();
A. push ("apple ");
A. push ("peach ");
A. push ("banala ");
The 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:
The Code is as follows:
Var c = [];
C ["a"] = "apple ";
C ["B"] = "banala ";
Or
The 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:
The Code is as follows:
Alert (c ["a"]);
"Apple" is displayed ".
To traverse, you can use:
The Code is as follows:
for(var key in c)
alert(c[key]);
In this way, all attribute values are displayed.
Of course, there is an each traversal in jquery, and you can also access each attribute and value. But this is only
var c={};If it is
var c=[];
No.
Then use
$.each(c, function(key, val) {
alert(key+":"+val);
});
It is convenient to use objects. It is much faster than to use arrays. 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.