The grep () method is used for array element filtering filtering.
grep (Array,callback,boolean); method parameters are described.
Array---pending arrays
Callback---This callback function is used to process each element in the array and filter the element, which contains two parameters, the first parameter is the value of the current array element, the second parameter is the subscript of the current array element, and the return value is a Boolean value.
Here is the source code for the grep () method:
//grep function, the third parameter indicates whether to reverse the results of FN! Grep:function(Elems, callback, invert) {varCallbackinverse, Matches=[], I= 0, //Save Array NumberLength =Elems.length,//reverse the second parameter passed in, and true turns False,false into true //If invert is false, that is,!inverse is true, then the element directly returning the function to True is added, so the desired callbackexpect is trueCallbackexpect =!invert; //Go through the array, only saving the items //That pass the validator function for(; i < length; i++ ) { //if invert is false to indicate no inversion, then if FN returns True, it is added directly to the matches array //gets the inverse result of the return value of the callback function resultCallbackinverse =!callback (elems[i], i); //This code indicates that the return value of the callback function is bool if(Callbackinverse!==callbackexpect) {Matches.push (elems[i]); } } returnmatches; }
Invert---The default is false, when the value is False, the array returned by grep () is an array that satisfies the callback condition, and when True, the grep () method returns an array that does not satisfy the callback condition.
When the value of invert is true. The code is as follows:
$ (function () { varfunction (value, index) { return Value > 5; },true); alert (result);
result=[0,1,2,3,4,5]
When the value of invert is false. The code is as follows
$ (function () { varfunction (value, index) { return Value > 5; },false); alert (result); });
RESULT=[6];
When the value of invert is not filled, the default is False.
Introduction to the Grep () method of Jquery traversal array