Demo the array used
Copy Code code as follows:
var _mozi=[' mohism ', ' Mo-tse ', ' Modi ', ' love not attack ', ' still with Shangxian '];
1. $.each traversal example [common]
Copy Code code as follows:
$.each (_mozi,function (key,val) {
The callback function has two parameters, the first is the element index, and the second is the current value
Alert (' _mozi array, index: ' +key+ ' corresponds to the value: ' +val ');
});
2. $.grep () filter array [Common]
Copy Code code 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 (' array value for Mozi's subscript is: ' +key ');
}
});
var _mozigt1=$.grep (_mozi,function (Val,key) {
Return key>1;
});
The element with an index value greater than 1 in alert (' _mozi array is: ' +_MOZIGT1);
var _mozilt1=$.grep (_mozi,function (Val,key) {
Return key>1;
},true);
The third reliable parameter is passed here, and the return value in the filter function is reversed
Alert (' _mozi an element with index value less than or equal to 1 is: ' +_MOZILT1 ');
3. $.map () convert array by given condition [general]
Copy Code code as follows:
var _maparra=$.map (_mozi,function (val) {
Return val+ ' [new addition] ';
});
var _maparrb=$.map (_mozi,function (val) {
Return val== ' Mozi '? ' [to Meziga] ' +val:val;
});
var _maparrc=$.map (_mozi,function (val) {
To extend a new element to an array element
return [Val, (val+ ' [extended]]];
});
Alert (' After each element is appended \ ' [New]\ '] The array is: ' + _maparra ');
Alert (' only adds the character to the element Mozi after the array is: ' + _MAPARRB);
Alert (' For each element in the original array, extends an element that adds a character \ ' [New]\ '], the returned array is ' +_MAPARRC ');
4. $.inarray () Determine whether the value exists in the array [common]
Copy Code code as follows:
var _exist=$.inarray (' Mozi ', _mozi);
var _inexistence=$.inarray (' Wei ', _mozi)
if (_exist>=0) {
Alert (' Mozi exists in the array _mozi, whose 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 () merge two arrays [general]
Copy Code code as follows:
Native concat () may be simpler than that.
_mozinew=$.merge (_mozi,[' ghost millet ', ' Shang Yang ', ' bin ', ' Pangjuan ', ' Su-Qin ', ' Zhang Yi '])
Alert (' The new array length after merging is: ' +_mozinew.length+ '. Its value is: ' +_mozinew ');
6. $.unique () filter the repeating elements in the array [not used]
Copy Code code as follows:
var _h2arr=$.makearray (h2obj);
Repeat the array _h2arr once
_h2arr=$.merge (_h2arr,_h2arr);
var _curlen=_h2arr.length;
_h2arr=$.unique (_h2arr);
var _newlen=_h2arr.length;
Alert (' array _h2arr original length value: ' +_curlen+ ', filtered as: ' +_newlen
+ '. Co-filtration ' + (_curlen-_newlen) + ' repeat element ')
7. $.makearray () class array objects are converted to arrays [infrequently used]
Copy Code code as follows:
var _makearr=$.makearray (h2obj);
Alert (the data type of the ' H2 element object collection is converted to: ' +_makearr.constructor.name ');
8. $.toarray () Restores all DOM elements to an array [not common]
Copy Code code as follows:
var _toarr=$ (' H2 '). ToArray ();
The data type of alert (' H2 element collection restored is: ' +_toarr.constructor.name ');