jQuery API

jQuery.grep()

jQuery.grep( array, function(elementOfArray, indexInArray) [, invert] ) Returns: Array

Description: Finds the elements of an array which satisfy a filter function. The original array is not affected.

  • version added: 1.0jQuery.grep( array, function(elementOfArray, indexInArray) [, invert] )

    arrayThe array to search through.

    function(elementOfArray, indexInArray)The function to process each item against. The first argument to the function is the item, and the second argument is the index. The function should return a Boolean value. this will be the global window object.

    invertIf "invert" is false, or not provided, then the function returns an array consisting of all elements for which "callback" returns true. If "invert" is true, then the function returns an array consisting of all elements for which "callback" returns false.

The $.grep() method removes items from an array as necessary so that all remaining items pass a provided test. The test is a function that is passed an array item and the index of the item within the array. Only if the test returns true will the item be in the result array.

The filter function will be passed two arguments: the current array item and its index. The filter function must return 'true' to include the item in the result array.

Examples:

Example: Filters the original array of numbers leaving that are not 5 and have an index greater than 4. Then it removes all 9s.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  p { color:green; margin:0; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div></div>
  <p></p>
  <span></span>
  
<script>
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(", "));

</script>

</body>
</html>

Demo:

Example: Filter an array of numbers to include only numbers bigger then zero.

$.grep( [0,1,2], function(n,i){
   return n > 0;
 });

Result:

[1, 2] 

Example: Filter an array of numbers to include numbers that are not bigger than zero.

$.grep( [0,1,2], function(n,i){
    return n > 0;
},true);

Result:

[0] 

Support and Contributions

Need help with jQuery.grep() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.grep()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • rwbaskette

    How about creating an alias to this called jQuery.filter that takes the parameters in the opposite order to stay consistent with the each function:

    jQuery.each( collection, callback(indexInArray, valueOfElement) )
    jQuery.filter( collection, callback(indexInArray, valueOfElement) )

    When you go back and forth frequently the parameter ordering causes the occasional head-scratching moment when debugging something.

    • http://twitter.com/mentat_enki Scott Ziegler

      Not to mention the fact that this method has absolutely NOTHING to do with global regular expression printing or parsing, which is what grep stands for in the first place.

      Its like some child who equates the word 'grep' with filtering wrote the API…. really shameful.

      I completely agree with rebaskette. Stick to a consistent pattern when doing API design please.

      • kurrent

        I posted a comment a year ago asking them to consider changing the very misleading and confusing name for this method.

        My comment was deleted a few days later for no reason .

        • http://www.learningjquery.com/ Karl Swedberg

          Not sure where you posted the comment, but if you posted it here on the API documentation site (which wouldn't have been possible a year ago, because it didn't exist in this format at that time and there was no commenting system in place), then it was probably deleted because this isn't the place to dispute the merits of API naming. If you want to carry on a discussion/debate/complaint about the naming, please follow the instructions in the big pink box: “Discussions about the API specifically should be addressed in the Developing jQuery Core forum.” Thanks.

  • bob

    Why include the invert parameter? Seems that it would make it confusing for someone debugging code if they notice in the grep function something like

    return n == 0;

    when, if the invert parameter is set, it would be something like

    return n != 0;

    when the invert parameter is not set. It just seems like something that would be better handled right in the callback function than in a third parameter.

  • http://www.learningjquery.com/ Karl Swedberg

    Not sure where you posted the comment, but if you posted it here on the API documentation site (which wouldn't have been possible a year ago, because it didn't exist in this format at that time and there was no commenting system in place), then it was probably deleted because this isn't the place to dispute the merits of API naming. If you want to carry on a discussion/debate/complaint about the naming, please follow the instructions in the big pink box: “Discussions about the API specifically should be addressed in the Developing jQuery Core forum.” Thanks.