This article mainly introduces Jquery's Array Operation Skills. For more information, see
1. $. each (array, [callback]) traversal [common] explanation: Unlike the $. each () method. This method can be used to sample any object (not just an array ~). The callback function has two parameters: the first is the object's member or array index, and the second is the corresponding variable or content. if you want to exit the each loop, the callback function returns false. Other return values are ignored. each traversal is a variant of the for Loop in normal event processing, but it is more powerful than the for loop. in the array, it can easily obtain the array index and the corresponding value. for example, the Code is as follows: var _ mozi = ['mojie', 'mozi', 'mozi', 'both love and unattacking ', 'still similar to Shang xian']; // The array used in this article, which 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 in the alert ('_ mozi array, index: '+ key +' corresponds to '+ val);}); relative to native .. in, each is 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]) filters arrays [commonly used] explanation: Filter array elements using the filter function. this function must pass at least two parameters (the third parameter is true or false, and the return value of the filter function is reversed, which is of little use): the array to be filtered and the filter function. the filter function must return true to retain the element or false to delete the element. in addition, the filter function can also be set as a string (which is not recommended for personal use and can be checked by yourself). The Code is as follows: $. 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 (the subscript of 'array value is: '+ key) ;}}); var _ moziGt1 = $. grep (_ mozi, function (val, key) {return key> 1 ;}); alert (the element whose index value is greater than 1 in the '_ mozi array is: '+ _ MoziGt1); var _ moziLt1 = $. grep (_ mozi, function (val, key) {return key> 1 ;}, true); // The third reliable parameter is input here, return values in the filter function are reversed by alert (the element with the index value less than or equal to 1 in the '_ mozi array is' + _ moziLt1); 3. $. map (array, [callback]) converts arrays based on given conditions [general] explanation: the conversion function as a parameter is called for each array element, in addition, a converted element is passed to the conversion function as a parameter. the conversion function can return the converted value, null (delete items in the array), or an array containing values, and extend it to the original array. this is a very powerful method, but it is not commonly used. it can update array element values based on specific conditions, or expand a new copy element based on the original value. the Code is as follows: var _ mapArrA = $. map (_ mozi, function (val) {return v Al + '[new addition]';}); var _ mapArrB = $. map (_ mozi, function (val) {return val = 'mozi '? '[Add only to Mozi]' + val: val;}); var _ mapArrC = $. map (_ mozi, function (val) {// expands a new element return [val, (val + '[extension]')] For the array element;}); alert ('the array after each element is appended with the \ '[new addition] \' character is: '+ _ mapArrA ); alert ('the array after only adding characters to the element Mozi is '+ _ mapArrB); alert (' is each element in the original array, expand an element that adds the character \ '[new] \', and the returned array is '+ _ mapArrC); 4. $. inArray (val, array): determines whether a value exists in an array. [common] explanation: determines the position of the first parameter in an array, count from 0 (if not found, return-1 ). do you remember the indexOf () method? IndexOf () returns the first occurrence position of the string, while $. inArray () returns the position of the input parameter in the array. Similarly, if found, a value greater than or equal to 0 is returned. If not found,-1 is returned. now, you know how to use it. with it, it becomes easy to determine whether a value exists in an array. the Code is as follows: var _ exist = $. inArray ('mozi ', _ mozi); var _ inexistence = $. inArray ('wei Yang ', _ mozi) if (_ exist> = 0) {alert ('mozi exists in array _ mozi, and its index value in the array is: '+ _ exist);} if (_ inexistence <0) {alert ('wei Yang does not exist in array _ mozi !, Returned value: '+ _ inexistence + '! ');} 5. $. merge (first, second) merges two arrays [general] explanation: the returned results will modify the content of the first array-the elements of the first array are followed by the elements of the second array. this method replaces the native concat () method with the jQuery method, but the function is not concat () Powerful. concat () can merge multiple arrays at the same time. the Code is as follows: // native concat () may be more concise than it _ moziNew = $. merge (_ mozi, ['guimigu ', 'shangyang', 'sunning', 'ponjuan ', 'suqin', 'zhangyi ']) alert ('the length of the new array after merging is '+ _ moziNew. length + '. the value is '+ _ moziNew); 6. $. unique (array) filters repeated elements in an array [not commonly used] explanation: deletes repeated elements in an array. only the DOM element array is deleted, but the string or number array is not processed. the first time I saw this method, I thought it was a very convenient method. It can be used to filter duplicates. What a perfect method is, but if I take a closer look, I can only process DOM elements. off. therefore, I defined it as an uncommon element. At least, I have never used it since I used jQuery. the Code is as follows: var _ h2Arr = $. makeArray (h2obj); // repeat array _ h2Arr once _ h2Arr = $. merge (_ h2Arr, _ h2Arr); var _ curLen = _ h2Arr. length; _ h2Arr = $. unique (_ h2Arr); var _ newLen = _ h2Arr. length; alert (the original length value of 'array _ h2Arr is '+ _ curLen +', which is '+ _ newLen +' After filtering '. filter '+ (_ curLen-_ newLen) + 'repeated elements') 7. $. makeArray (obj) converts a class array object to an array [not commonly used] explanation: converts a class array object to an array object. The class array object has the length attribute, the member index is 0 to length-1. this is a redundant method. The omnipotent $ already contains this function. the official jQuery website is very vague. in fact, it is to convert a class array object (for example, the element object set obtained using getElementsByTagName) into an array object. the Code is as follows: var _ makeArr = $. makeArray (h2obj); alert ('h2 element object set data type conversion: '+ _ makeArr. constructor. name); // output Array 8. $ (dom ). toArray () restores all DOM elements to an array [not commonly used] explanation: restores all DOM elements in the jQuery set to an array. This is not a common method. I even think it and $. makeArray is redundant. the Code is as follows: var _ toArr = $('h2 '). toArray (); alert (the data type after the 'h2 element set is restored: '+ _ toArr. constructor. name );