jQuery | grep() Method

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html>
  
<head
    <title
        JQuery | grep() method
    </title>
      
    <script src=
    </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>

chevron_right


Output:

Example 2: In this example, the grep() method is applies on the array of JavaScript objects to filter out some objects based on the condition. This method doesn’t affect the original array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html>
      
<head
    <title
        JQuery | grep() method
    </title>
      
    <script src=
    </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>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.