jQuery grep() Method Last Updated : 31 Jul, 2024 Comments Improve Suggest changes Like Article Like Report This grep() method in jQuery is used to finds the elements of an array that satisfies a filter function. Syntax:jQuery.grep(array, function(element, index) [, invert])Parameters: This method accepts two parameters as mentioned above and described below: array: This parameter holds the array like object for searching.function(element, index): It is the filter function which takes two arguments, element which holds the element of the array and index which holds the index of that particular element.invert: It is false or not passed, then the function returns an array having all elements for which "callback" returns true. If this is passed true, then the function returns an array having all elements for which "callback" returns false.Return Value: It return the elements which satisfies the filter function. Example 1: In this example, the grep() method is applies on the array of numbers to filter some numbers based on the condition. It doesn't affect the original array. html <!DOCTYPE html> <html> <head> <title> JQuery | grep() method </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP" style = "font-size: 20px; font-weight: bold"> </p> <button onclick = "GFG_Fun();"> click here </button> <p id="GFG_DOWN" style = "font-size: 26px; font-weight: bold; color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ]; up.innerHTML = "Click on the button to " + "perform the operation.<br>" + "Array - <br>[" + arr + "]"; function GFG_Fun() { var d = $.grep(arr, function( n, i ) { return ( n !== 7 && i > 4 ); }); down.innerHTML = JSON.stringify(d); } </script> </body> </html> Output:Example 2: In this example, the grep() method is applied on the array of JavaScript objects to filter out some objects based on the condition. This method doesn't affect the original array. html <!DOCTYPE html> <html> <head> <title> JQuery | grep() method </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP" style= "font-size: 20px; font-weight: bold"> </p> <button onclick = "GFG_Fun();"> click here </button> <p id="GFG_DOWN" style="font-size: 26px; font-weight: bold; color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var data = [ {"prop_1":"val_11", "prop_2":"val_12"}, {"prop_1":"val_21", "prop_2":"val_22"}, {"prop_1":"val_11", "prop_2":"val_22"}, {"prop_1":"val_61", "prop_2":"val_52"}, {"prop_1":"val_21", "prop_2":"val_52"}, {"prop_1":"val_61", "prop_2":"val_12"} ]; up.innerHTML = "Click on the button to " + "perform the operation.<br>" + "JSON - <br>" + JSON.stringify(data); function GFG_Fun() { var d = $.grep(data, function(n, i){ return n.prop_1==='val_11'; }); down.innerHTML=JSON.stringify(d); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article jQuery grep() Method P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JQuery jQuery-Methods Similar Reads JQuery | isArray() method This isArray() Method in jQuery is used to determines whether the argument is an array. Syntax: jQuery.isArray( object ) Parameters: The isArray() method accepts only one parameter that is mentioned above and described below: object : This parameter is the object to test whether or not it is an arra 2 min read jQuery inArray() method This inArray() Method in jQuery is used to search for a specific value in an array and return its index (or -1 if not found). SyntaxjQuery.inArray(val, arr [, Index])Parameters The inArray() method accepts three parameters that are described below: val: The value to search in an array.arr: Any array 1 min read JQuery | type() method This type() Method in jQuery is used to determine the internal JavaScript [[Class]] of an object. Syntax: jQuery.type( obj ) Parameters: The type() method accepts only one parameter that is mentioned above and described below: obj: This parameter is the object to get the internal JavaScript [[Class] 2 min read jQuery | contains() Method This contains() method in jQuery is used to check whether a DOM element is a descendant of another DOM element or not. Syntax: jQuery.contains( container, contained ) Parameters: This method accepts two parameters as mentioned above and described below: container: This parameter holds the DOM elemen 1 min read JQuery | parseJSON() method This parseJSON() method in jQuery takes a well-formed JSON string and returns the resulting JavaScript value. Syntax: jQuery.parseJSON( json )Parameters: The parseXML() method accepts only one parameter that is mentioned above and described below: json: This parameter is the well-formed JSON string 2 min read jQuery :contains() Selector The jQuery :contains() selector in jQuery is used to select elements containing the specified string. Syntax: $(":contains(text)")Parameters: This selector contains single parameter text which is mandatory and used to specify the text to find. Example 1: This example uses :contains() selector to sel 1 min read JavaScript Array indexOf() Method The indexOf() method in JavaScript is used to find the position of the first occurrence of a specific value in an array. If the value is not present, it returns -1. This method is handy for quickly determining where a particular item is located within an array.Syntax:array.indexOf(element, start)Par 3 min read Collect.js search() Method The search() method is used to search the given element in collection and returns its key if it exists. If the element is not found then it returns false. Syntax: collect(array).search(element) Parameters: The collect() method takes one argument that is converted into the collection and then search( 1 min read Lodash _.findKey() Method Lodash _.findKey() method is similar to the _.find() method except that it returns the key of the first element, and the predicate returns true instead of the element itself. Syntax:_.findKey(object, [predicate])Parameters:object(Object) holds the object to inspect every element.predicate(Function) 2 min read Lodash _.find() Method The Lodash _.find() method searches through a collection (array or object) and returns the first element that satisfies a specified condition (predicate). Itâs useful for quickly locating a matching item within a collection. If no match is found, it returns undefined.Syntax_.find(collection, predica 2 min read Like