As we all know, Jquery is an efficient encapsulation of JavaScript, so the array to be operated by Jquery is an array in JavaScript. in JavaScript, we use for and for-in for Array Operations, in Jquery, $. map (), $. each () is used to operate an array. First, it is an ordinary array (an array with an index of an integer ):
$. Map (arr, fn );
Call the fn function to process each element in the array one by one. The fn function returns a new array.
The Code is as follows:
Var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
Var newarr = $. map (arr, function (item) {return item * 2 });
Alert (newarr );
$. Each (array, fn) calls the fn function to process each element of the array. No return value is returned.
The Code is as follows:
Var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$. 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.
The Code is as follows:
Var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$. 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:
The Code is as follows:
Var arr = {"jim": "11", "tom": "12", "lilei": "13 "};
$. 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:
The Code is as follows:
<% @ WebHandler Language = "C #" Class = "Handler" %>
Using System;
Using System. Web;
Using System. Web. Script. Serialization;
Using System. Collections. Generic;
Public class Handler: IHttpHandler {
Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/plain ";
Person p1 = new Person {Age = "22", Name = "tom "};
Person p2 = new Person {Age = "23", Name = "jim "};
Person p3 = new Person {Age = "24", Name = "lilei "};
IList Persons = new List {P1, p2, p3 };
JavaScriptSerializer js = new JavaScriptSerializer ();
String s = js. Serialize (persons );
Context. Response. Write (s );
}
Public class Person
{
Public string Name {get; set ;}
Public string Age {get; set ;}
}
Public bool IsReusable {
Get {
Return false;
}
}
}
Three person objects are instantiated first, then put into a collection, and finally serialized into a string to flow to the client;
Client:
The Code is as follows: