1. $.each (array, [callback]) traversal [common]
Explanation: Unlike the $ (). each () method of the JQuery object, this method can be used to sample any object. The callback function has two parameters: the first is the index of the member or array of the object, and the second is the corresponding variable or content. If you need to exit each loop so that the callback function returns false, the other return values are ignored.
Each traversal, believed to be new, is a variant of the For loop in normal event handling, but is more powerful than a for loop. In an array, it is easy to capture the array index and its corresponding value. Cases:
The code is as follows |
Copy Code |
var _mozi=[' mohism ', ' Mo-tse ', ' Modi ', ' love not attack ', ' still with Shangxian ']; The array used in this article is the same as $.each (_mozi,function (key,val) { The callback function has two parameters, the first is the element index, and the second is the current value Alert (' _mozi array, index: ' +key+ ' corresponds to the value: ' +val '); }); |
In relation to the native for.. In,each a little stronger. For.. In can also traverse the array and return the corresponding index, but the value needs to be obtained through Arrname[key].
2. $.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.
The code is as follows |
Copy Code |
$.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 '); |
3. $.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.
The code is as follows |
Copy Code |
var _maparra=$.map (_mozi,function (val) { Return val+ ' [new addition] '; }); var _maparrb=$.map (_mozi,function (val) { Return val== ' Mozi '? ' [to Meziga] ' +val:val; }); var _maparrc=$.map (_mozi,function (val) { To extend a new element to an array element 9 return [Val, (val+ ' [extended]]]; 10 }); 11 Alert (' After each element is appended with the ' [New plus] ' character, the array is: ' + _maparra '); 12 Alert (' only adds the character to the element Mozi after the array is: ' + _MAPARRB); 13 Alert (' For each element in the original array, extends an element that adds a character ' [New] ', and the returned array is ' +_MAPARRC '); |
4. $.inarray (Val,array) determines whether the value exists in the array [common]
Explanation: Determines the position of the first parameter in the array, counting from 0 (if not found, returns-1).
Remember the indexof () method? IndexOf () returns the first occurrence of the string, and $.inarray () returns the position of the passed-in argument in the array, and, if found, returns a value greater than or equal to 0, or 1 if not found. Now, you know how to use it. With it, it becomes easy to judge whether a value exists in an array.
The code is as follows |
Copy Code |
var _exist=$.inarray (' Mozi ', _mozi); var _inexistence=$.inarray (' Wei ', _mozi) if (_exist>=0) { Alert (' Mozi exists in the array _mozi, whose index value in the array is: ' +_exist '); } if (_inexistence<0) { Alert (' Wei does not exist in the array _mozi!, the return value is: ' +_inexistence+ '! '); } |
5. $.merge (First,second) merges two arrays [general]
Interpretation: The returned result modifies the contents of the first array-the elements of the first array followed by the elements of the second array. This method replaces the native Concat () method with jquery, but the functionality is not concat () powerful, and concat () can combine multiple arrays at the same time.
The code is as follows |
Copy Code |
Native concat () may be simpler than that. _mozinew=$.merge (_mozi,[' ghost millet ', ' Shang Yang ', ' bin ', ' Pangjuan ', ' Su-Qin ', ' Zhang Yi ']) Alert (' The new array length after merging is: ' +_mozinew.length+ '. Its value is: ' +_mozinew '); |
6. $.unique (array) filter the repeating elements in an array [not common]
Explanation: Deletes the repeating element in the array. Handles only the delete DOM element array, not the string or numeric array.
See this method for the first time, think this is a very convenient method, can filter repetition, how perfect. But with a closer look, only the DOM elements are processed, and functionality is 80 percent. So, I've defined it as a less common element, at least, I haven't used it since jquery.
The code is as follows |
Copy Code |
var _h2arr=$.makearray (h2obj); Repeat the array _h2arr once _h2arr=$.merge (_h2arr,_h2arr); var _curlen=_h2arr.length; _h2arr=$.unique (_h2arr); var _newlen=_h2arr.length; Alert (' array _h2arr the original length value: ' +_curlen+ ', Filtered by: ' +_newlen + '. Total filter ' + (_curlen-_newlen) + ' duplicate element ') |
7. $.makearray (obj) converts class array objects to arrays [not common]
Explanation: Converts an array object of a class to an array object with a length property and a member index of 0 to Length-1.
This is a redundant method, and an omnipotent $ would have included this feature. The jquery official web interpretation is very vague, in fact, it is a class array objects (such as the collection of element objects obtained by getElementsByTagName) to transform the group of objects.
The code is as follows |
Copy Code |
var _makearr=$.makearray (h2obj); Alert (the data type of the ' H2 element object collection is converted to: ' +_makearr.constructor.name ');/output Array |
8. $ (DOM). ToArray () Restores all DOM elements to an array [not common]
Explanation: Restores all DOM elements in the jquery collection to an array. The method is not commonly used, and the individual even feels that it is as superfluous as $.makearray.
Ar _toarr=$ (' H2 '). ToArray ();
The data type of alert (' H2 element collection restored is: ' +_toarr.constructor.name ');