The operation techniques of the jquery array

Source: Internet
Author: User
Tags array length arrays constructor extend

  This article mainly introduces the operation techniques of the jquery array, and the friends who need it can refer to the following

1. $.each (array, [callback]) traversal [Common]    Interpretation: Unlike the $.each () method of the JQuery object, this method can be used to iterate over any object (not just the array). 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, the callback function returns FALSE. Other return values will be ignored .  each traversal, which is not unfamiliar, is a variant of the For loop in normal event handling, but is more powerful than a for loop. In an array, it can easily capture array indices and corresponding values. :  code is as follows: Var _mozi=[' mohism ', ' Mozi ', ' Modi ', ' love not attack ', ' still with Shangxian ']; The array used in this article, the same as   $.each (key,val) { //callback function has two parameters, the first is the element index, the second is the current value   alert (' _mozi array, index: ' +key+ ' corresponding value is: ' +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 is required to obtain;  2 via Arrname[key]. $.grep (Array, callback, [invert]) filter array [Common]  explanation: Filters array elements using filter functions. 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 also be set to a note string (personally not recommended, to learn to read);  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 is Mozi's subscript is: ' +key); } });  var _mozigt1=$.grep (_mozi, function (Val,key) {  return key>1; });nbsp 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 the 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]  Explanation: The conversion function as a parameter is invoked for each array element, and the conversion function is passed an element that represents the converted. 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 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 is as follows: Var _maparra=$.map (_mozi,function (val) {  return val+ ' [new addition] '; } ';  var _maparrb=$.map (_mozi,function (val) {  return val== ' Mozi '? ' [Meziga] ' +val:val; } ';  var _maparrc=$.map (_mozi,function (val) { //The array element expands a new element   return [Val, ( Val+ ' [Extended] ')]; });  alert (' The array after each element with the ' [New plus] ' character is: ' + _maparra ');  alert (' The array that only adds characters to the element Mozi 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]   Explanation: Determine 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 is as follows: Var _exist=$.inarray (' Mozi ', _mozi);  var _inexistence=$.inarray (' Wei ', _ Mozi)   if (_exist>=0) {  alert (' Mozi exists in the array _mozi, the 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) Merge two arrays [General]  Explanation: The result returned modifies the contents of the first array-the elements of the first array followed by the elements of the second array .  This approach is to use jquery to replace the native concat () method, but the function is not concat () powerful, concat () can merge multiple arrays at the same time .    code as follows://native concat () may be more concise than it   _ 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 [not common]  interpretation: Deletes duplicate elements in an array. Handles only the deletion of a DOM element array, not a string or a numeric array .  the first time I saw this method, I thought it was a very convenient way to filter the repetition, ha, how perfect, but look carefully, only to handle the DOM elements. function 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: Var _h2arr=$.makearray (H2OBJ); //_h2arr The array once again   _h2arr=$.merge (_h2arr,_h2arr);  var _curlen=_h2arr.length;  _h2Arr=$. Unique (_h2arr);  var _newlen=_h2arr.length;  alert (' array _h2arr the original length value is: ' +_curlen+ ', after filtering: ' +_newlen  + '. Total filter ' + (_curlen-_newlen) + ' repeat element ')     7. $.makearray (obj) converts class array objects to arrays []  Explanation: Convert class array objects to array objects, class array objects have length properties, and their members are indexed to 0 to length-1.  this is an extra method, omnipotent $ This feature is already included. The jquery official online explanation is very vague. Instead, it converts an array of objects, such as a collection of element objects obtained with getElementsByTagName, to the .  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 []  explanation: Restoring all the DOM elements in the jquery collection to a;  is not a common method, and the individual feels it is as superfluous as $.makearray.     Code is as follows: Var _toarr=$ (' H2 '). ToArray ();  alert (' H2 the data type after recovery of the element collection is: ' +_toarr.constructor.name ');   

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.