Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
jQuery | get() Method
Next article icon

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:


Next Article
jQuery | get() Method

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JQuery
  • jQuery-Methods

Similar Reads

  • jQuery | get() Method
    In jQuery .get() method loads data from the server by using the GET HTTP request. This method returns XMLHttpRequest object. Syntax $.get( url, [data], [callback], [type] ) Parameters url : String containing the URL at which request is to be sent data : This is an optional parameter that represents
    2 min read
  • jQuery getScript() Method
    The getScript() method in jQuery is used to run a JavaScript using AJAX HTTP GET request. Syntax: $(selector).getScript(url, success(response, status))Parameters: It contains two parameters as mentioned above and described below: url: It is a required parameter. It holds the url to whom the request
    2 min read
  • jQuery has() Method
    The has() is an inbuilt method in jQuery which is used to find all the elements inside the specified list of elements. Syntax: $(selector).has(element)Parameter: It accepts a parameter expression or an element to match elements against them. Return Value: It returns all elements that match the speci
    1 min read
  • 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
    2 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 hasData() method
    This hasData() method in JQuery is used to determine whether an element has any jQuery data associated with it. This data may be text, event associated with element. There are two examples discussed below: Syntax: jQuery.hasData(element)Arguments: element: This parameter is a DOM element which is to
    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 | isXMLDoc() Method
    This isXMLDoc() Method in jQuery is used to check to see if a DOM node is within an XML document. Syntax: jQuery.isXMLDoc( node ) Parameters: This method accept a single parameter which is mentioned above and described below: node: This parameter holds the DOM node that will be checked to see if it'
    1 min read
  • jQuery hasClass() Method
    The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exist or not. Syntax: $(selector).hasClass(className);Parameter: It accepts a "className" parameter which specifies the class name needed to search in the selected element. Return Value: It r
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences