jQuery in Action
Manipulating JavaScript objects and collections
171
Let's say that we want to filter an array for all values that are greater than 100. We
do that with a statement such as the following:
do that with a statement such as the following:
var bigNumbers = $.grep(originalArray,function(value) {
return value > 100;
});
The callback function that we pass to
$.grep()
can use whatever processing it
likes to determine if the value should be included. The decision could be as easy
as this example or, perhaps, even as complex as making synchronous Ajax calls
(with the requisite performance hit) to the server to determine if the value should
be included or excluded.
as this example or, perhaps, even as complex as making synchronous Ajax calls
(with the requisite performance hit) to the server to determine if the value should
be included or excluded.
When the decision making is as simple as this example, jQuery provides a
shortcut that we can use to make the statement more compact--provide the
expression as a string. For example, we can rewrite our code snippet as
expression as a string. For example, we can rewrite our code snippet as
var bigNumbers = $.grep(originalArray,'a>100');
Function syntax: $.grep
$.grep(array,callback,invert)
Traverses the passed array invoking the callback function for each element. The return value
of the callback function determines if the value is collected into a new array returned as the
value of the $.grep() function. If the invert parameter is omitted or false, a callback
value of true causes the data to be collected. If invert is true, a callback value of false
causes the value to be collected.
The original array isn't modified.
of the callback function determines if the value is collected into a new array returned as the
value of the $.grep() function. If the invert parameter is omitted or false, a callback
value of true causes the data to be collected. If invert is true, a callback value of false
causes the value to be collected.
The original array isn't modified.
Parameters
array
(Array) The traversed array whose data values are examined for collection.
This array isn't modified in any way by this operation.
This array isn't modified in any way by this operation.
callback
(Function|String) A function whose return value determines if the current data
value is to be collected. A return value of true causes the current value to be
collected, unless the value of the invert parameter is true in which case
the opposite occurs.
This function is passed two parameters: the current data value and the index
of that value within the original array.
A string can also be passed as this parameter that's converted into the
value is to be collected. A return value of true causes the current value to be
collected, unless the value of the invert parameter is true in which case
the opposite occurs.
This function is passed two parameters: the current data value and the index
of that value within the original array.
A string can also be passed as this parameter that's converted into the
callback function. See the following discussion for details.
invert
(Boolean) If specified as true, it inverts the normal operation of
the function.
Returns
The array of collected values.
The array of collected values.