Home > Article > Web Front-end > Introduction to jquery array filtering method grep()_jquery
There is a grep() method in jquery for filtering array elements. Unfortunately, this description cannot be found in the API documentation we usually use. View the official instructions: http://api.jquery.com/jQuery.grep/
How to use grep():
grep(array,callback,invert)
array: array to be filtered;
callback: Process each element in the array and filter the elements. This function contains two parameters, the first is the value of the current array element, and the other is the subscript of the current array element, that is, the element index value. This function should return a boolean value. Alternatively, this function can be set to a string, and when set to a string, is treated as a "lambda-form" (short form?), where a represents the array element and i represents the element index value. For example, "a > 0" represents "function(a){ return a > 0; }"
invert: Boolean optional, default value false, value is true or false. If "invert" is false or set, the function returns the element in the array that is returned true by the filter function. When "invert" is true, Returns the set of elements that return false in the filter function.
After explaining the usage of grep(), let’s give a small example:
So what is returned now is [0,1,2], which is the element filtered out by the callback function.