grep vs jQuery过滤? [英] Grep vs Filter in jQuery?
问题描述
将匹配元素的集合减少为与选择器匹配的元素,或
传递函数的测试。
Grep:
查找满足过滤器
函数的数组的元素。原始数组不受影响。
确定。
$ b $所以如果我在GREP中这样做的话:
$ $ p $ $ $ c $ var arr = [1,9,3,8,6, 1,5,9,4,7,3,8,6,9,1];
myNewArray = jQuery.grep(arr,function(n,i){
return(n!= 5& i> 4);
});
我也可以这样做:
<$ p $变量arr = [1,9,3,8,6,1,5,9,
myNewArray = $(arr).filter(function(n,i){
return(n!= 5& i> 4);
}) ;
在这两种情况下,我仍然可以访问原始数组...
so ...在哪里有区别?
不同的用法。过滤函数用于html元素,这就是为什么它是一个可链接的函数,它返回一个jQuery对象,它接受像:even这样的过滤器, ,:odd或:visible等等。你不能用grep函数来实现这个功能,而grep函数是用来作为数组的实用函数的。
I was wondering about the differences between Grep and Filter :
Filter :
Reduce the set of matched elements to those that match the selector or pass the function's test.
Grep :
Finds the elements of an array which satisfy a filter function. The original array is not affected.
ok.
so if I do this in GREP :
var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
myNewArray= jQuery.grep(arr, function(n, i){
return (n != 5 && i > 4);
});
I could do also :
var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
myNewArray= $(arr).filter( function(n, i){
return (n != 5 && i > 4);
});
In both situations I still can access to the original array...
so...where is the difference ?
They both function in similar ways however they differ in their usages.
The filter function is intended to be used with html elements, and that is why it is a chainable function that returns a jQuery object and it accepts filters like ":even", ":odd" or ":visible" etc. You can't do that with the grep function, which is intended to be a utility function for arrays.
这篇关于grep vs jQuery过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!