As we all know, LAN chat software is an efficient encapsulation of JavaScript, so the arrays to be operated by jquery are the arrays in JavaScript, in JavaScript, we use for and for-in for Array Operations, while in jquery, we use $. map (), $. each () to operate the array:
The first is an ordinary array (an array with an index of integers ):
$. Map (ARR, FN );
Call the FN function to process each element in the array one by one. The FN function returns a new array.
View sourceprint? 1 var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
2 var newarr = $. Map (ARR, function (item) {return item * 2 });
3 alert (newarr );
$. Each (array, FN) calls the FN function to process each element of the array. No return value is returned.
View sourceprint? 1 var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
2 $. Each (ARR, function (Key, value) {alert ("key:" + key + "value:" + value );});
You can also omit the function parameter. In this case, you can obtain the value of the current element to be traversed.
View sourceprint? 1 var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
2 $. Each (ARR, function () {alert (this );});
Then there is an array of key-value pairs indexed as strings. For such arrays,
$. Each (array, FN) is generally used for operations:
View sourceprint? 1 var arr = {"Jim": "11", "Tom": "12", "lilei": "13 "};
2 $. Each (ARR, function (Key, value) {alert ("name:" + key + "Age:" + value );});
Of course, you can also use a function without parameters to traverse;
This type of data can be obtained from the server as follows:
Server:
View sourceprint? 01 <% @ webhandler Language = "C #" class = "handler" %>
02
03 using system;
04 using system. Web;
05 using system. Web. Script. serialization;
06 using system. Collections. Generic;
07 Public class handler: ihttphandler {
08
09 public void processrequest (httpcontext context ){
10 context. response. contenttype = "text/plain ";
11 person p1 = new person {age = "22", name = "Tom "};
12 person P2 = new person {age = "23", name = "Jim "};
13 person P3 = new person {age = "24", name = "lilei "};
14 ilist persons = new list {P1, P2, P3 };
15. javascriptserializer JS = new javascriptserializer ();
16 string S = Js. serialize (persons );
17 context. response. Write (s );
18}
19
20 public class person
21 {
22 public string name {Get; set ;}
23 Public String age {Get; set ;}
24}
25 public bool isreusable {
26 get {
27 return false;
28}
29}
30
31}
Three person objects are instantiated first, then put into a collection, and finally serialized into a string to flow to the client;
Client:
View sourceprint? 01
02
03
04
05
06
07
08
15
16
17
18
19
The client uses $. parsejson () to convert the string passed in the background to a JS array object. Next, we use the Normal Array Operation Method to operate the obtained array.
The third is the jquery object array obtained through the tag selector,
View sourceprint? 01
02
03
04
05
06
07
08
13
14
15
16
17
18
19
The effect of running in a browser is:
After Dom loading is complete, the text is dynamically added to each P element. First, $ ("p") gets the set of P tags, which is equivalent to the document in JavaScript. getelementbytagname is only the array of jquery objects obtained here, so that jquery's inherent implicit iteration function is available, followed by text ("this is the P tag ") the operation is iterated to every p tag. We can also call the each function to display the jquery object array obtained by the iteration. The following code can also achieve the above effect:
View sourceprint? 01
02
03
04
05
06
07
08
15
16
17
18
19
20
21