jQuery.grep( array, function [, 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 [, invert ] )
-
arrayType: ArrayLikeObjectThe array-like object to search through.
-
functionThe 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. -
invertType: BooleanIf "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.
-
$.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.
Examples:
Example 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
|
Demo:
Example 2
1
2
3
|
|
Result:
1
|
|
Example 3
1
2
3
|
|
Result:
1
|
|