Jquery Array Operations

Source: Internet
Author: User

1. Creation of arrays

var arrayobj = new Array (); Create an array

var arrayobj = new Array ([size]); Create an array and specify the length, note not the upper limit, is the length

var arrayobj = new Array ([element0[, element1[, ... [, ELEMENTN]]]); Create an array and assign a value

To illustrate, although the second method creates an array that specifies the length, the array is actually variable in all cases, meaning that even if the length is 5, the element can still be stored outside the specified length, note that the length changes accordingly.

2. Access to elements of an array

var testgetarrvalue=arrayobj[1]; Gets the element value of the array

Arrayobj[1]= "This is the new value"; Assigning a new value to an array element

3. Adding array elements

Arrayobj. Push ([Item1 [item2] [... [Itemn]]); /Adds one or more new elements to the end of the array and returns the new length of the array

Arrayobj.unshift ([Item1 [item2 [...] [Itemn]]); /Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, returning the new length of the array

Arrayobj.splice (insertpos,0,[item1[, item2[, ... [, Itemn]]]); /inserts one or more new elements into the specified position of the array, the element at the insertion position automatically moves back, and returns "".

4. Deletion of array elements

Arrayobj.pop (); Removes the last element and returns the element value

Arrayobj.shift (); Removes the first element and returns the element value, and the elements in the array are automatically moved forward

Arrayobj.splice (Deletepos,deletecount); Removes the specified number of DeleteCount elements from the specified position deletepos, returning the removed element as an array

5. Interception and merging of arrays

Arrayobj.slice (start, [end]); Returns a portion of the array as an array, noting that the element of end is not included, and if the end is omitted, all elements after start are copied

Arrayobj.concat ([item1[, item2[, ... [, Itemn]]]); Concatenate multiple arrays (which can also be strings, or a mixture of arrays and strings) into an array, returning a new, well-connected array

6, copy of the array

Arrayobj.slice (0); Returns a copy array of the array, note that it is a new array, not a pointer to the

Arrayobj.concat (); Returns a copy array of the array, note that it is a new array, not a pointer to the

7. Sorting of array elements

Arrayobj.reverse (); Reverses the element (the top row to the last and last to the top), returning the array address

Arrayobj.sort (); Sort the array elements, returning the address of the arrays

8. String of array elements

Arrayobj.join (separator); Returns a string that connects each element value of an array, separated by separator.

toLocaleString, toString, valueOf: Can be seen as a special use of joins, not commonly used

9. Filtering arrays

$.grep (Array, callback, [invert]) filters the array, and this function passes at least two arguments (the third argument is true or false, indicating whether to reverse): array to filter and filter function. The filter function must return true to preserve the element or false to delete the element

grep (Array,callback,invert)

    • Array: arrays to be filtered;
    • Callback: Processes each element in the array and filters the element, which contains two parameters, the first is the value of the current array element, and one is the subscript of the current array element, the element index value, and this function should return a Boolean value such as $.grep (Arr,function (value, Index) {return index>1;});. In addition, this function can be set to a string that, when set to a string, is considered "Lambda-form" (abbreviated form?). ), where a represents an array element, and I represents the index value of an element. such as "a > 0" means "function (a) {return a > 0;}"
    • Invert: boolean selectable, default value false, value TRUE or false. If "invert" is false or set, the function returns the element in the array that is true by the filter function, and when "invert" is true, returns the set of elements that return false in the filter function.

  

Extended:

1. Use grep to delete the specified value

var arr = [12345]; var 2 ;      = $.grep (arr, function (value) {    return value! = RemoveItem;});

2. Array traversal

Unlike the $ (). each () method of the JQuery object, this method can be used to sample any object (not just an array, oh ~). The callback function has two parameters: the first is the index of the object's member or array, and the second is the corresponding variable or content. If you need to exit each loop to have the callback function return False, the other return values will be ignored

var_mozi=['Mohist School','Mozi','Modi','and love non-offensive','Shang Tong Shangxian'];//the array used in this article, the same as$.each (_mozi,function (key,val) {//The callback function has two parameters, the first is an element index, the second is the current valueAlert'_mozi Array, index:'+key+'the corresponding values are:'+val); }); 

3. $.map (Array,[callback]) convert an array by a given condition [general]
Explanation: The conversion function as a parameter is called for each array element, and the conversion function is passed an element that represents the transformation as an argument. The conversion function can return a converted value, null (delete an item in an array), or an array containing a value, and extend to the original array.
This is a very powerful method, but it is not commonly used. It can update the value of an array element based on a specific condition, or extend a new copy element based on the original value.

var_maparra=$.map (_mozi,function (val) {returnval+'[New addition]';});var_maparrb=$.map (_mozi,function (val) {returnval=='Mozi'?'[for Meziga only]'+Val:val;});var_maparrc=$.map (_mozi,function (val) {//expands a new element for an array element    return[Val, (val+'[Extended]')];}); Alert ('after each element add \ ' [New plus]\ ' character to the array:'+_maparra); Alert ('the array to which only the element Mozi is added is:'+_MAPARRB); Alert ('for each element in the original array, expand one of the added characters \ ' [new add]\ ' element, the returned array is'+_MAPARRC);
4. $.inarray (Val,array) determine if the value exists in the array [common]

Explanation: Determines the position of the first parameter in the array, counting from 0 (returns-1 if not found).
Remember the indexof () method? IndexOf () returns the first occurrence of a string, while $.inarray () returns the position of the passed parameter in the array, and, similarly, if found, returns a value greater than or equal to 0, or 1 if not found.

var_exist=$.inarray ('Mozi', _mozi);var_inexistence=$.inarray ('Wei', _mozi)if(_exist>=0) {alert ('Mozi exists in the array _mozi, and its 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 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 function is not concat () powerful, and concat () can combine multiple arrays at the same time.

//native concat () may be more concise than it is._mozinew=$.merge (_mozi,['Ghost Millet','Shang Yang','Sun Bin','Pangjuan','Su Qin','Zhang Yi']) alert ('the new array length after merging is:'+_mozinew.length+'. Its value is:'+_mozinew);

6. $.unique (array) filter repeating elements in an array [infrequently used]
Explanation: Deletes the repeating element in the array. Handles only the deletion of an array of DOM elements, not a string or a numeric array.

var_h2arr=$.makearray (h2obj);//repeats 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 is:'+_curlen+', filtered to:'+_newlen+'. co-filtration'+ (_curlen-_newlen) +'a repeating element')

7. $.makearray (obj) converts class array objects to arrays [infrequently used]
Explanation: The class array object is converted to an array object with the Length property and its member index is 0 to length-1.
This is a redundant method, the omnipotent $ would have included this feature. The jquery official online explanation is very vague. In fact, it is the conversion of a class array object (such as a collection of element objects obtained with getElementsByTagName) to an object of the tuple.

var _makearr=$.makearray (h2obj); alert ('+_makearr.constructor.name ); //
8. $ (DOM). ToArray () Restores all DOM elements to an array [infrequently used]

Explanation: Restores all DOM elements in the jquery collection to an array; It is not commonly used, and individuals even feel it is as superfluous as $.makearray.

var _toarr=$ ('h2'). ToArray (); alert ('  '+_toarr.constructor.name);

Reference:

Http://www.open-open.com/lib/view/open1372393730385.html

Http://www.jb51.net/article/57883.htm

Jquery Array Operations

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.