-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathjQuery.grep.xml
83 lines (80 loc) · 2.92 KB
/
jQuery.grep.xml
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?xml version="1.0"?>
<entry type="method" name="jQuery.grep" return="Array">
<title>jQuery.grep()</title>
<desc>Finds the elements of an array which satisfy a filter function. The original array is not affected.</desc>
<signature>
<added>1.0</added>
<argument name="array" type="ArrayLikeObject">
<desc>The array-like object to search through.</desc>
</argument>
<argument name="function" type="Function">
<argument name="elementOfArray" type="Object" />
<argument name="indexInArray" type="Integer" />
<return type="Boolean" />
<desc>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. <code>this</code> will be the global window object.</desc>
</argument>
<argument name="invert" optional="true" type="Boolean">
<desc>If "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.</desc>
</argument>
</signature>
<longdesc>
<p>The <code>$.grep()</code> 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.</p>
<p> 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.</p>
</longdesc>
<example>
<desc>Filters the original array of numbers leaving that are not 5 and have an index greater than 4. Then it removes all 9s.</desc>
<code><![CDATA[
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( ", " ) );
]]></code>
<css><![CDATA[
div {
color: blue;
}
p {
color: green;
margin: 0;
}
span {
color: red;
}
]]></css>
<html><![CDATA[
<div></div>
<p></p>
<span></span>
]]></html>
</example>
<example>
<desc>Filter an array of numbers to include only numbers bigger then zero.</desc>
<code><![CDATA[
$.grep( [ 0, 1, 2 ], function( n, i ) {
return n > 0;
});
]]></code>
<results><![CDATA[
[ 1, 2 ]
]]></results>
</example>
<example>
<desc>Filter an array of numbers to include numbers that are not bigger than zero.</desc>
<code><![CDATA[
$.grep( [ 0, 1, 2 ], function( n, i ) {
return n > 0;
}, true );
]]></code>
<results><![CDATA[
[ 0 ]
]]></results>
</example>
<category slug="utilities"/>
<category slug="version/1.0"/>
</entry>