Jquery knowledge point 2 jquery operations on arrays _ jquery

Source: Internet
Author: User
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:






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.