The jQuery. grep () function is used to filter elements in an array using the specified function and return the filtered array. The source array is not affected. The filter result is only reflected in the returned result array. This function is a global jQuery object. The jQuery. grep () function is used to filter elements in an array using the specified function and return the filtered array.
The source array is not affected. The filter result is only reflected in the returned result array.
This function is a global jQuery object.
Syntax
JQuery 1.0 adds this static function.
JQuery. grep (array, function [, invert])
Parameters
Note:
This function traverses the array elements and executes the filter function. It provides two parameters for function: one is the array element of the current iteration, and the other is the index of the current iteration element in the array.
If the invert parameter is not specified or the value is false, the result array contains all elements that function returns true. If the invert parameter is true, the result array contains all elements that function returns false.
Return Value
The Return Value of the jQuery. grep () function is of the Array type, and an Array of Results filtered by the specified function is returned.
Example & Description
The jQuery sample code of the jQuery. grep () function is as follows:
// Append the line feed label and the specified HTML content to the current page. function w (html) {document. body. innerHTML + ="
"+ Html;} var arr = [10, 25, 3, 0,-3, 20, 6, 8, 11]; function filter (value, index) {return value> 10;} var result = $. grep (arr, filter); // retains all elements greater than 10 w (result); //, 11var result2 = $. grep (arr, filter, true); // retains all elements not greater than 10 w (result2); //, 0,-, 8