The first is an ordinary array (an array with an index of integers):
$.map(arr,fn);
Call the fn function on each element in the array to process it one by one. The fn function will process and return the final result. A new array
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, and there is no return value
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 parameters of function. At this time, this can get the value of the current element traversed
var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each(arr, function() { alert(this); });
Then the key value indexed as a string For arrays,
generally uses $.each(array,fn) to operate:
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;
When this Class data can be obtained from the server side as follows:
Server side:
<%@ 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;
}
}
}
First Three person objects are instantiated, then put into a collection, and finally the collection is serialized into a string and streamed to the client;
Client:
< ;head>