jQuery API jQuery API 中英文对照版 - jQuery在线查询手册
$.grep(array, fn, inv)
$.grep(array,fn,inv)

使用筛选函数,从一个数组中筛选项目。 其中筛选函数必须传递两个参数:数组中的当前项目和数组中项目的索引。如果要保持数组中的项目,这个函数必须返回true;如果返回false,就会删除项目。

返回值:Array

参数:

  • array (Array): 要在其中查找项目的数组
  • fn (Function): 处理数组项目的函数
  • inv (Boolean): 反转选项 - 选择相反的筛选结果
 

示例:

$.grep( [0,1,2], function(i){ return i > 0; });

结果:

[1, 2]

 
$.grep( array, fn, inv )

Filter items out of an array, by using a filter function.

The specified function will be passed two arguments: The current array item and the index of the item in the array. The function must return 'true' to keep the item in the array, false to remove it.

Return value: Array
Parameters:

  • array (Array): The Array to find items in.
  • fn (Function): The function to process each item against.
  • inv (Boolean): Invert the selection - select the opposite of the function.
 

Example:

 $.grep( [0,1,2], function(n,i){     return n > 0;   });  
Result:
 [1, 2]