jQuery 中的 Grep 与过滤器? [英] Grep vs Filter in jQuery?
问题描述
我想知道 Grep 和 Filter 之间的区别:
I was wondering about the differences between Grep and Filter :
过滤器:
将匹配元素的集合减少到与选择器匹配的元素或通过函数的测试.
Reduce the set of matched elements to those that match the selector or pass the function's test.
格雷普:
查找满足过滤条件的数组元素功能.原数组不受影响.
Finds the elements of an array which satisfy a filter function. The original array is not affected.
好的.
所以如果我在 GREP 中这样做:
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);
});
我也可以:
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...
那么……区别在哪里?
推荐答案
它们的功能相似,但用法不同.
They both function in similar ways however they differ in their usages.
过滤器函数旨在与 html 元素一起使用,这就是为什么它是一个可链接的函数,它返回一个 jQuery 对象,并且它接受像 ":even"、":odd" 或 ":visible" 等过滤器.你不能用 grep 函数做到这一点,它旨在成为数组的实用函数.
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.
这篇关于jQuery 中的 Grep 与过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!