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 = [5]; 2; arr =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‘,‘Mozi ", Modi ', ' and love non-offensive ", ' ' +
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) {Return val+‘[New Addition]‘;});var _maparrb=$.map (_mozi,function (val) {Return val==‘Mozi‘ ?‘[For Meziga only]' +Val:val;});var _maparrc=$.map (_mozi,function (val) {//return [Val, (Val+ ' ' + _MAPARRB); alert ( ' " +_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 if (_exist>=0 ' 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 a little more concise than it is _mozinew=$.merge (_mozi,[‘" " Shang Yang Sun bin ', ' Pangjuan ', "+_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);//$.merge (_h2arr,_h2arr); var _curlen=_h2arr.length;_h2arr=$.unique (_h2arr); var _newlen=_h2arr.length;alert ( ' array _h2arr original length value: ' +_curlen+ ' +_newlen+< Span style= "color: #800000;" > ' " + (_curlen-_newlen) + ' 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