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 ');