> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/jquery-or-grep-method-ok.md).

# jQuery | grep() Method (ok)

Phương thức grep () này trong jQuery được sử dụng để tìm các phần tử của một mảng thỏa mãn hàm lọc.

```
jQuery.grep(array, function(element, index) [, invert])
```

```
<!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>
```

```
<!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> 
```

```
<script type="text/javascript">
  jQuery(document).ready(function($) {
    var data = {
      "items": [{
          "id": 1,
          "category": "cat1"
        },
        {
          "id": 2,
          "category": "cat2"
        },
        {
          "id": 3,
          "category": "cat1"
        }
      ]
    };
    dataf = $.grep(data.items, function(element, index) {
      return element.id == 1;
    });
    console.log(dataf);
    // [{ "id": 1, "category": "cat1" }]
  });
  </script>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/jquery-or-grep-method-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
