JQuery Operation Array each, map, grep, filter

Source: Internet
Author: User

There are several methods to operate arrays in Jquery:
1. $. each (collection, callback (indexInArray, valueOfElement ))
The $. each () function is different from the $ (selector). each () function. $. The each () function can be used to traverse any set, whether it is a JavaScript object or an array. If it is an array, each time the callback function passes the subscript of an array and the value of the array corresponding to this subscript (this value can also be obtained through the this keyword in the function body, however, JavaScript usually treats this value as an object. Even if it is just a simple string or a number, this function returns the traversed object, which is the first parameter of this function, note that here is the original array, which is different from map.
Collection indicates the target array, callback indicates the callback function (defined by yourself), the first parameter of the callback function is the subscript of the array, and the second parameter is the element of the array. Of course, we can also set only one parameter for the callback function. This parameter must be a subscript, and no parameter is acceptable.
The following is an example:
Var mem = ['A', 'B', 'C'];
Var returnvalue = $. each (mem, function (index ){
Alert (index );
});
Or
Var mem = ['A', 'B', 'C'];
Var returnvalue = $. each (mem, function (index, value ){
Alert (index + "" + value );
});
Or
Var mem = ['A', 'B', 'C'];
Var returnvalue = $. each (mem, function (){
});

2. $. map (array, callback (elementOfArray, indexInArray ))
$. The map () function is very similar to the each function. The most important difference is that map is used to generate a new array, that is to say, we can modify every element retrieved in the callback function (return a new value through return, and no value will be returned without return), and finally return a new array.
There is basically no difference between parameters and each.
The following is an example:
Var arr = ["a", "B", "c", "d", "e"];
Var arr2 = jQuery. map (arr, function (n, I ){
Return (n. toUpperCase () + I );
}); Www.2cto.com
Alert (arr );
Alert (arr2 );
The execution result of the program is:
A, B, c, d, e
A0, B1, C2, D3, E4

3. $. grep (array, function (elementOfArray, indexInArray) [, invert])
$. The grep () function removes unnecessary elements and acts as a filter. By passing its elements through a test function, that is, the second parameter of grep, note that the parameters here are in the opposite order of the callback functions of the previous two functions. These two parameters are used to control which elements you need and return them through the return Statement (return true indicates that this element is required ). In addition, in grep (), you can use the regular expression (return elementOfArray. match (Regular Expression) to control filtering, which makes filtering more flexible and powerful. The grep () and map () functions generate a new array. The third parameter in grep () is a Boolean which is usually omitted. The default value is false. If it is set to true, the return value of the callback function is the opposite, if the return value is true, it is removed.
The following is an example:
Var arr = [1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1];
Arr = jQuery. grep (arr, function (n, I ){
Return (n! = 5 & I> 4 );
});
4. The filter (fn) parameter is a function;


Author: cqkxzyi

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.