Latest web development tutorials

jQuery.grep () Método

Métodos variados jQuery Métodos variados jQuery

Exemplos

Filtrando a matriz original

<Div> </ div> < p> </ p> <span> </ span> <script> (função $ () { var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ]; $ ( "Div" ). Texto ( arr. juntar-se ( "," ) ) ;. Arr = jQuery grep ( arr, function ( n, i ) { retorno ( n! == 5 && i> 4 );}); $ ( "P" ). Texto ( arr. juntar-se ( "," ) ) ;. Arr = jQuery grep ( arr, function ( um ) { retorno ! A == 9;}); $ ( "Span" ). Texto ( arr. juntar-se ( "," ) );}) </ Script>

tente »

Definição e Uso

$ Função .grep () usa os elementos filtrantes função especificada na matriz e retorna uma matriz filtrada.

Dica: matriz de origem não é afetado, filtro de resultados refletem apenas os resultados da matriz retornada.


gramática

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

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


Exemplos

mais exemplos

Filtrando a matriz original
Filtrar o valor do elemento da matriz for maior que 0.

Filtrando a matriz original
Filtrar o valor do elemento da matriz não é maior que 0.


Métodos variados jQuery Métodos variados jQuery