Summary of common jQuery Data Processing Methods

Source: Internet
Author: User

Summary of common jQuery Data Processing Methods

This article mainly introduces common jQuery data processing methods. Examples summarize common jQuery data processing methods such as trim, param, isArray, isFunction, and each, which has some reference value, for more information, see

 

 

This example summarizes common jQuery data processing methods. Share it with you for your reference. The details are as follows:

$. Trim (): removes spaces before and after a string.

?

1

2

Var str = 'fries ';

Var formatStr = $. trim (str); // 'fries'

$. Param (): serialize an array or object.

?

1

2

3

4

5

Var obj = {

Name: 'Dog ',

Count: 10

};

Var str = $. param (obj); // "name = dog & count = 10"

$. IsArray (): checks whether it is an array.
$. IsFunction (): checks whether the function type is used.
$. Each (obj, [callback]): traverses an array or a collection object. Obj is the set object to be traversed. callback indicates the callback function, which starts when every Member is traversed. The callback function contains two parameters: the first parameter is the index of the object member or array, and the second parameter is the corresponding value.

?

1

2

3

4

5

6

7

8

9

10

Var a = [x, y, z];

$. Each (a, function (index, value ){

Console. log (index); // 0, 1, 2

Console. log (value); // x, y, z

});

Var B = {x: 1, y: 2, z: 3 };

$. Each (B, function (key, value )){

Console. log (key); // x, y, z

Console. log (value); // 1, 2, 3

}

Note: If you want to exit the each () loop halfway, you can return false in the callback function. Other return values will be ignored.

$. Each () and $ (selector ). each () functions are the same, but $. each () can traverse any object or array, while $ (selector ). each () can only traverse the jQuery object selected by the current selector.

$. MakeArray (): converts a class array object to an array object. The so-called class array object has the length attribute, and the index ranges from 0 to length-1. However, these objects cannot call the array method.

?

1

2

Var arr = $. makeArray ($ ('lil'); // convert a class array object to an array

Response ('ul'response .html (arr. reverse (); // you can call the reverse () method of the array.

$. Grep (): filters out elements that do not meet the conditions in the Array Based on the filter function.

$. Grep (array, callback, [invert]);

Description: The array parameter indicates the array to be filtered, and callback indicates the filter function. If the filter function returns true, the elements are retained. If the filter function returns false, the elements are deleted. The invert parameter is optional. If it is false or not set, the elements returned by the filter function in the array are true. If this parameter is true, the elements returned by the filter function are false.

?

1

2

3

4

5

6

7

8

Var a = [1, 2, 3, 4, 5];

A = $. grep (a, function (value, index )){

Return value> 3; // a is [4, 5]

});

Var B = [1, 2, 3, 4, 5];

B = $. grep (B, function (value, index )){

Return value> 3; // B is now [1, 2, 3]

}, True );

$. Map (): map an array.
$. Map (array, callback );

?

1

2

3

4

Var a = [1, 2, 4];

A = $. map (a, function (elem )){

Return elem * 2; // a is now [2, 4, 6, 8]

});

$. Merge (): merges arrays.
Description: Two array parameters are accepted.

?

1

2

3

Var a = [1, 2, 3];

Var B = [4, 5];

Var c = $. merge (a, B); // c is [1, 2, 3, 4, 5];

$. Unique (): deletes repeated items in the DOM element array.

?

1

2

3

4

5

6

7

8

9

...

<A id = "1" class = "link link1"> </a>

<A id = "2" class = "link"> </a>

<A id = "3" class = "link"> </a>

...

Var $ link = $ ('. link'); // contains #1, #2, #3

Var $ firstLink = $ ('. link1'); // contains #1

Var $ links = $. merge ($ link, $ firstLink); // contains #1, #2, #3, #1

Var $ linkList = $. unique ($ link); // contains #1, #2, #3

Note: Only DOM element arrays can be processed. strings or JavaScript arrays cannot be processed.

I hope this article will help you with jQuery programming.

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.