The
jquery array is processed, convenient, and fully functional. One-Step encapsulates a number of features that are not possible with native JavaScript arrays. The following is an explanation of the use of jquery arrays, the need for friends can refer to the following
1. $.each (array, [callback]) traversal [common] explanation: Different from the $ (). each () method of the JQuery object, this method can be used to iterate over 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, believe is not unfamiliar, in the ordinary event processing, is for the loop variant, but more powerful than the for loop. In an array, it is easy to capture the array index and its corresponding value. Example: code is as follows: Var _mozi=[' mohism ', ' Mo-tse ', ' Modi ', ' love not attack ', ' still with Shangxian ']; The array used in this article, the same below $.each (_mozi,function (key,val) { //callback function has two parameters, the first is the element index, the second is the current value & nbsp Alert (' _mozi array, index: ' +key+ ' corresponds to the value: ' +val '); }); relative to 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 array elements 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) : To filter arrays and filter functions. 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. Code as follows: $.grep (_mozi,function (val,key) { //filter function has two parameters, the first is the current element, 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; }); Alert (the element with index value greater than 1 in the ' _mozi array is: ' +_MOZIGT1); var _mozilt1=$.grep (_mozi,function (val,key) { return key>1; },true); //Here a third reliable parameter is passed, and the return value in the filter function is reversed alert (the element with index value less than or equal to 1 in the ' _mozi array is: ' +_mozilt1); 3. $.map (Array,[callback]) converts an array by a given condition [general] interpretation: 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. Code as follows: Var _maparra=$.map (_mozi,function (val) { return val+ ' [Add] '; }); var _maparrb=$.map (_mozi,function (val) { return val== ' Mozi '? ' [to Meziga] ' +val:val; }); var _maparrc=$.map (_mozi,function (val) { //array element extension A new element Return [Val, val+ ' [extended] ']; }); Alert (' After each element is appended with the ' [New plus] ' character, the array is: ' + _maparra '); ALert (' Only add characters to the element Mozi after the array is: ' + _MAPARRB); Alert (' For each element in the original array, extending an element that adds a character ' [New] ', the returned array is ' +_MAPARRC '); 4. $.inarray (Val,array) determines whether the value exists in the array [common] interpretation: Determines the position of the first parameter in the array, counting from 0 (if not found, return-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. Code as follows: Var _exist=$.inarray (' Mozi ', _mozi); var _inexistence=$.inarray (' Wei ', _mozi) if (_exist>=0) { alert (' Mozi exists in array _mozi, Its index value in the array is: ' +_exist '; } if (_inexistence<0) { alert (' Wei not present in array _mozi!), the return value is: ' +_inexistence+ '! '); } &N Bsp 5 $.merge (First,second) Merge two arrays [general] Explanation: The returned result modifies the contents of the first array-the elements of the first array followed by the element 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. Code is as follows://native concat () may be more concise than it points _mozinew=$.merge (_mozi,[' ghost millet ', ' Shang Yang ', ' bin ', ' Pangjuan ', ' Su-Qin ', ' Zhang Yi ']) alert (' The new array length after merging is: ' +_mozinew.length+ '. The value is: ' +_mozINEW); 6 $.unique (array) filters repeating elements in an array [infrequently used] Explanation: Deletes a repeating element in an array. Handles only the delete DOM element array, not the string or numeric array. For the first time saw this method, 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. Code as follows: Var _h2arr=$.makearray (h2obj); //Repeat the array _h2arr _h2arr=$.merge (_h2arr,_h2arr); VAR _curlen=_h2arr.length; _h2arr=$.unique (_h2arr); VAR _newlen=_h2arr.length; Alert (' array _h2arr original length value: ' +_curlen+ ', Filtered by: ' +_newlen + '. Filter ' + (_curlen-_newlen) + ' repeating element ' ) 7. $.makearray (obj) converts class array objects to arrays [infrequently] interprets: Converts an array object of a class to an array object with a length property and a member index of 0 through 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. Code as follows: 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 the DOM elements in the jquery collection to one. Not commonly used in methods that individuals even feel it and $.maKearray is just as superfluous. Code as follows: Var _toarr=$ (' H2 '). ToArray (); Alert (the data type after the ' H2 element collection is restored: ' +_toarr.constructor.name ');