YouTip LogoYouTip

Misc Grep

# jQuery.grep() Method [![Image 4: jQuery Misc Methods](#)jQuery Misc Methods](#) ## Example Filter the original array
$(function(){var arr = [1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1]; $("div").text(arr.join(", ")); arr = jQuery.grep(arr, function(n, i){return(n !== 5&&i>4); }); $("p").text(arr.join(", ")); arr = jQuery.grep(arr, function(a){return a !== 9; }); $("span").text(arr.join(", ")); }) [Try it Β»](#) * * * ## Definition and Usage The $.grep() function filters an array using a specified function and returns the filtered array. **Note:** The source array is not affected; the filtering result is only reflected in the returned result array. * * * ## Syntax _$_.grep( array, function [, invert ] ) | Parameter | Description | | :--- | :--- | | _array_ | Array type. The array to be filtered. | | _function_ | Function type. The specified filter function. The grep() method provides two parameters to the function: the first is the current array element being iterated, and the second is the index of that element in the array. | | _invert_ | Optional. Boolean type. Default is false. Specifies whether to invert the filtering result. If the parameter invert is true, the result array will contain all elements for which the function returns false. | * * * ![Image 5: Example](#) ## More Examples (#) Filter out elements in the array with values greater than 0. (#) Filter out elements in the array with values not greater than 0. * * jQuery Misc Methods](#)
← Misc IsarrayDocker Install Php β†’