jquery $.grep map array filtering and transformation

Source: Internet
Author: User

jquery $.grep map array filtering and transformation

$.grep (Array, callback, [invert]) filter array [Common]
Explanation: Filter The elements of an array using a filter function. This function passes at least two arguments (the third argument is true or false, and the filter function returns a value that is not useful to the individual): an array to filter and a filter function. The filter function must return true to preserve the element or false to delete the element. In addition, the filter function can be set to a note string (individuals do not recommend, to understand their own lookup);

$.grep (_mozi,function (Val,key) {
The filter function has two parameters, the first is the current element, and the second is the element index
if (val== ' Mozi ') {
Alert (' array value for Mozi's subscript is: ' +key ');
}
});

var _mozigt1=$.grep (_mozi,function (Val,key) {
Return key>1;
});
The element with an index value greater than 1 in alert (' _mozi array is: ' +_MOZIGT1);

var _mozilt1=$.grep (_mozi,function (Val,key) {
Return key>1;
},true);
The third reliable parameter is passed here, and the return value in the filter function is reversed
Alert (' _mozi an element with index value less than or equal to 1 is: ' +_MOZILT1 ');


$.map (Array,[callback]) converts an array by a given condition [general]
Explanation: The conversion function as a parameter is invoked for each array element, and the transformation function is passed an element that represents the transformation as an argument. A conversion function can return a converted value, null (delete an item from an array), or an array containing values, and extend to the original array.
This is a very powerful method, but it is not commonly used. It can update an array element value based on a specific condition, or extend a new replica element based on the original value.

Var _maparra=$.map (_mozi,function (val) {
 return val+ ' [new Add] ';
});
var _maparrb=$.map (_mozi,function (val) {
 return val== ' Mozi '? ' [to Meziga] ' +val:val;
});
var _maparrc=$.map (_mozi,function (val) {
 //expands a new element for an array element
 return [Val, (val+ ' [extension]]];
});
alert (' After each element is appended with the ' [New plus] ' character, the array is: ' + _maparra ');
Alert (' The array that only adds characters to the element Mozi is: ' + _MAPARRB);
Alert (' For each element in the original array, extends an element that adds a character ' [New] ', and the returned array is ' +_MAPARRC ');

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.