Latest web development tutorials

jQuery.grep () method

jQuery Misc Methods jQuery Misc Methods

Examples

Filtering the original array

<Div> </ div> < p> </ p> <span> </ span> <script> $ (function () { 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>

try it"

Definition and Usage

$ .grep () Function uses the specified function filter elements in the array and returns an array filtered.

Tip: source array is not affected, filter results reflect only the results of the array returned.


grammar

$.grep( array, function [, invert ] )

参数 描述
array Array类型 将被过滤的数组。
function Function类型 指定的过滤函数。grep()方法为function提供了两个参数:其一为当前迭代的数组元素,其二是当前迭代元素在数组中的索引。
invert 可选。 Boolean类型 默认值为false,指定是否反转过滤结果。如果参数invert为true,则结果数组将包含function返回false的所有元素。


Examples

More examples

Filtering the original array
Filter out the array element value is greater than 0.

Filtering the original array
Filter out the array element value is not greater than 0.


jQuery Misc Methods jQuery Misc Methods