jQuery.grep()S2C Home « Utilities « jQuery.grep()

Array object creation.

Description

The jQuery.grep() jQuery General utility method, creates a new array, from the elements of an existing array meeting the requirements of a filter function, without affecting the contents of the original array.

Shorthand version $.grep()

Syntax

Signature Description
jQuery.grep( array, function(elementOfArray,
         indexInArray) [, invert] )
Creates a new array, from the elements of an existing array meeting the requirements of a filter function, without affecting the contents of the original array, optionally inverting the returned array contents.

Parameters

Parameter Description Type
arrayThe array to interrogate.Array
function(elementOfArray, indexInArray)A function to process on each element within the defined array parameters.
elementOfArray - The current array item.
indexInArray - The array index value.
  • The callback is fired in the context of the global window, so the keyword this refers to the global window object.
Function
inverseA boolean value.
false - ( default ) Returns an array consisting of all elements for which the 'callback' returns true.
true - Returns an array consisting of all elements for which the 'callback' returns false.
Boolean

Return

An Array object.

jQuery.grep( array, function(elementOfArray,
                    indexInArray) [, invert] )
ExamplesTop

Creates a new array, from the elements of an existing array meeting the requirements of a filter function, without affecting the contents of the original array, optionally inverting the returned array contents.

In the example below when we press the top button the first time we create an array of values which we pass through the jQuery.grep()General utility method. We set the elementOfArray parameter to != 2 and the indexInArray value to > 7. Therefore starting at index position '8' our new array will return all items to our new array that do not equal the value '2'.

When we press the bottom button the first time we create an array of values which we pass through the jQuery.grep()General utility method using the invert option. We set the elementOfArray parameter to != 2 and the indexInArray value to > 7. With the invert option set, our new array will only contain elements that return false. Therefore the new array will contain all elements before index position '8' and all elements after this index position that equal the value '2'.



$(function(){
  $('#btn5').one('click', function(){
    var arr0 = [ 7, 2, 2, 5, 3, 6, 0, 8, 9, 1, 9, 5, 6, 2, 2, 5 ];
    var arr1 = jQuery.grep(arr0, function(n, i) {
   	  return (n != 2 && i > 7);
    });
    $('#div5').append('The new array contains the following values: ' + arr1 + '<br>');
  });
  $('#btn6').one('click', function(){
    var arr0 = [ 7, 2, 2, 5, 3, 6, 0, 8, 9, 1, 9, 5, 6, 2, 2, 5 ];
    var arr1 = jQuery.grep(arr0, function(n, i) {
   	  return (n != 2 && i > 7);
    }, true );
    $('#div5').append('The new array contains the following values: ' + arr1 + '<br>');
  });
});

div5. Some initial text.

Press the button below to action the above code: